8
  _fbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(OnFetchPageNotification);
  _fbClient.GetAsync(_kNotificationPath, new Dictionary<string, object> { { "access_token", _kPageAccessToken } });

How to convert above code into awaitable code in wp7:

 object = await _fbClient.GetAsync(_kNotificationPath, new Dictionary<string, object> { { "access_token", _kPageAccessToken } });

I have CTP Installed and task parallel library also.

1 Answer 1

18

The Async CTP came with a document that describes how to adapt each existing pattern to the Task Based Async pattern. It says that the Event based one is more variable, but does give one example:

public static Task<string> DownloadStringAsync(Uri url)
{
    var tcs = new TaskCompletionSource<string>();
    var wc = new WebClient();
    wc.DownloadStringCompleted += (s,e) =>
    {
        if (e.Error != null) tcs.TrySetException(e.Error);
        else if (e.Cancelled) tcs.TrySetCanceled();
        else tcs.TrySetResult(e.Result);
    };
    wc.DownloadStringAsync(url);
    return tcs.Task;
}

Where the original function that's being wrapped is DownloadStringAsync, the parameters match the parameters being passed to this function, and DownloadStringCompleted is the event that is being monitored.


(The same document appears to be downloadable here - the above sample (and more description) are from "Tasks and the Event-based Asynchronous Pattern (EAP)")

4
  • I wonder if with this pattern you would need to unsubscribe from the DownloadStringCompleted event to avoid a memory leak or if the lambda is just a loose object on the heap. Commented Jul 5, 2013 at 16:48
  • @FilipSkakun - only just noticed your comment, so apologies for the delay in responding. You'll notice that the event is on the wc object which itself looks like it may be garbage collected soon. Whatever is happening inside DownloadStringAsync is presumably keeping the wc object alive enough that the event handler is even called. But once that's done, wc is surely eligible for garbage collection, and so, therefore, should be the delegate that was added to the event. Commented Mar 28, 2014 at 7:10
  • Just curious, why does it have to be a static method? Could we just write it as a private method?
    – frostshoxx
    Commented Jul 2, 2018 at 22:08
  • 1
    @frostshoxx - it doesn't "have" to be a static method. But given that the above code is self-contained and clearly doesn't depend on any instance state of whatever class it's contained in, static seems sensible. public/private is a separate choice and yes, it could easy be private if that was applicable. However, I was just copying an existing code sample and it was probably intended to end up in a "Utility" style class for helper functions. Commented Jul 3, 2018 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.