gpt4 book ai didi

c# - 将基于事件的模式转换为异步 CTP 模式

转载 作者:太空狗 更新时间:2023-10-29 20:53:06 28 4
gpt4 key购买 nike

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

如何将上面的代码转换成 wp7 中的可等待代码:

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

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

最佳答案

异步 ​​CTP 附带了一份文档,该文档描述了如何使每个现有模式适应基于任务的异步模式。它说基于事件的变量更多,但确实举了一个例子:

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是正在监视的事件。


(同一文档似乎可以下载 here - 上面的示例(以及更多描述)来自“任务和基于事件的异步模式 (EAP)”)

关于c# - 将基于事件的模式转换为异步 CTP 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12853445/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com