Skip to content

Instantly share code, notes, and snippets.

@DVDPT
Last active March 5, 2021 00:41
Show Gist options
  • Save DVDPT/5444388 to your computer and use it in GitHub Desktop.
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.
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