gpt4 book ai didi

web-services - WP8 SDK import Service Reference with task-based operations not possible

转载 作者:行者123 更新时间:2023-12-04 04:20:48 25 4
gpt4 key购买 nike

到目前为止,在 VS2012 中使用“生成基于任务的操作”导入服务引用似乎不起作用。它是灰色的。

针对 WPF 的新项目进行的测试工作正常 - 我可以选择基于任务的操作或异步操作。

有没有一种简单的方法可以将异步调用包装在任务中?

最佳答案

Is there a simple way on wrapping the async call in a task?

WebClient.DownloadStringCompleted 示例

public static class WebClientAsyncExtensions
{
public static Task<string> DownloadStringTask(this WebClient client, Uri address)
{
var tcs = new TaskCompletionSource<string>();

DownloadStringCompletedEventHandler handler = null;
handler = (sender, e) =>
{
client.DownloadStringCompleted -= handler;

if (e.Error != null)
{
tcs.SetException(e.Error);
}
else
{
tcs.SetResult(e.Result);
}
};

client.DownloadStringCompleted += handler;
client.DownloadStringAsync(address);

return tcs.Task;
}
}

用法:

async void DownloadExample()
{
WebClient client = new WebClient();
await client.DownloadStringTask(new Uri("http://http://stackoverflow.com/questions/13266079/"));
}

关于web-services - WP8 SDK import Service Reference with task-based operations not possible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266079/

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