转换基于事件模式异步模式CTP

转换基于事件模式异步模式CTP

本文介绍了转换基于事件模式异步模式CTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何上面code转换成awaitable code在WP7:

How to convert above code into awaitable code in wp7:

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

我已经安装了CTP和任务并行库还。

I have CTP Installed and task parallel library also.

推荐答案

的异步CTP附带介绍如何将每个现有模式的基于任务异步模式相适应的文件。它说,基于事件之一就是更加多变,但确实给了一个例子:

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;
}

如果说是被包装的原始功能是 DownloadStringAsync ,参数匹配传递给这个函数的参数,而 DownloadStringCompleted 是正被监视的事件

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.

(同一文件似乎是下载 - 上面的示例(更多介绍)从任务和基于事件的异步模式(EAP))

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

这篇关于转换基于事件模式异步模式CTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:18