Created
January 3, 2014 20:36
-
-
Save sleimanzublidi/8246105 to your computer and use it in GitHub Desktop.
Event To Async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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