-
-
Save DVDPT/5444388 to your computer and use it in GitHub Desktop.
A simple extension method to replace the async event based get requests from facebook C# sdk to a async task based.
This file contains 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
static class FbServiceClientAsync | |
{ | |
public static Task<FacebookApiEventArgs> DoGetAsync(this FacebookClient client, string getPath, object parameters = null) | |
{ | |
var tcs = new TaskCompletionSource<FacebookApiEventArgs>(); | |
var obj = new object(); | |
EventHandler<FacebookApiEventArgs> eventHandler = null; | |
eventHandler = (s, a) => | |
{ | |
// | |
// Only finish when the UserState is the same as the object created on | |
// method call, to prevent handling an incorrect callback. | |
// | |
if(!obj.Equals(a.UserState)) | |
return; | |
if (eventHandler != null) | |
client.GetCompleted -= eventHandler; | |
tcs.TrySetResult(a); | |
}; | |
client.GetCompleted += eventHandler; | |
client.GetAsync(getPath, parameters, obj); | |
return tcs.Task; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment