Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I´m trying to convert a "classic" asynchronous method that uses a callback into a 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 a UserState, and authClient_LoginCompleted is the callback.

I already have the logic for a async/await methods, the problem is that the interaction in windows phone with Microsoft.Live uses callbacks. I´m considering a solution using semaphore, in order not to change the logic I have. That could be a good option?

like image 970
user1845148 Avatar asked Nov 22 '12 14:11

user1845148


1 Answers

If you need to wrap asynchronous callbacks into Tasks, then you can use TaskCompletionSource<T>. MSDN has the full details.

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

LiveLoginResult result = await authClient.LoginAsync(new[] { "var1", "var2" });
like image 56
Stephen Cleary Avatar answered Sep 22 '22 14:09

Stephen Cleary