Convert / wrap a "classic" asynchronous method that uses a callback

I am trying to convert a "classic" asynchronous method that uses a callback to the async / await method.

This is the code:

authClient.LoginCompleted += authClient_LoginCompleted; authClient.LoginAsync(new List<string>() { "var1", "var2" }, data); static void authClient_LoginCompleted(object sender, LoginCompletedEventArgs e) { ... } 

Where " data " is UserState and authClient_LoginCompleted is the callback.

I already have the logic for the async / await methods, the problem is that the interaction on the Windows phone with Microsoft.Live uses callbacks. I am considering a solution using a semaphore so as not to change the logic. Could this be a good option?

+7
source share
1 answer

If you need to wrap asynchronous callbacks in Task s, you can use TaskCompletionSource<T> . MSDN contains complete information .

However, in your case, you can simply use LoginAsync without the UserState parameter:

 LiveLoginResult result = await authClient.LoginAsync(new[] { "var1", "var2" }); 
+10
source

All Articles