如何使"老式"异步方法awaitable [英] How to make an "old style" async method awaitable
本文介绍了如何使"老式"异步方法awaitable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个异步方法有回调
If I have an asynchronous method with a callback
MyMethodAsync( <Input Parameters ...>, Callback);
我怎样才能使它awaitable?
how can I make it awaitable?
[此方法对于Windows Phone 7的,但应同样适用于任何类似的C#构造]
[This method is for windows phone 7, but should be equally applicable to any similar c# construct]
DNSEndpoint Endpoint = ...
NameResolutionCallback Callback = (nrr) => { ... }
DeviceNetworkInformation.ResolveHostNameAsync(Enpoint, Callback, null);
我想提出一个awaitable包装解决这个电话,让我等待回调到下一个命令完成后再继续。
I want to put an awaitable wrapper around this call, so I wait for the callback to complete before continuing with the next command.
推荐答案
您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/dd449174.aspx\"><$c$c>TaskCompletionSource$c$c>:
var tcs = new TaskCompletionSource<TypeOfCallbackParameter>();
MyMethodAsync(..., r => tcs.SetResult(r));
return tcs.Task;
这篇关于如何使&QUOT;老式&QUOT;异步方法awaitable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文