Skip to content

Instantly share code, notes, and snippets.

@sleimanzublidi
Created January 3, 2014 20:36
Show Gist options
  • Save sleimanzublidi/8246105 to your computer and use it in GitHub Desktop.
Save sleimanzublidi/8246105 to your computer and use it in GitHub Desktop.
Event To Async
public async Task DoSomethingAsync()
{
var tsc = new TaskCompletionSource<object>();
EventHandler<AsyncResult> handler = (o, e) =>
{
if (e.Error == null)
{
tsc.TrySetResult(null);
}
else
{
tsc.TrySetException(e.Error);
}
};
try
{
var x = new serviceClient();
x.DoSomethingCompleted += handler;
x.DoSomethingAsync();
return await tsc.Task;
}
finally
{
x.DoSomethingCompleted -= handler;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment