Async CTP introduced a document that describes how to adapt each existing template to a task-based Async template. It says that an event-based event is more variable, but gives 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; }
If the original function that wraps is DownloadStringAsync , the parameters correspond to the parameters passed to this function, and DownloadStringCompleted is the event that is being monitored.
(The same document looks downloadable here - the above example (and a more detailed description) refers to "Tasks and Asynchronous Events Based on Events" Template (EAP) ")
source share