将现有的异步方法包装到 TPL 兼容方法中
如何将接受回调函数作为参数的现有异步方法包装到任务并行库兼容方法中?
// Existing method
void DoAsync(Action<string> callback) {
...
}
// The desired method should have similar prototype
Task<string> DoAsync() {
// Internally this method should call existing
// version of DoAsync method (see above)
}
How to wrap existing async method that accepts callback function as parameter into Task Parallel Library-compatible method?
// Existing method
void DoAsync(Action<string> callback) {
...
}
// The desired method should have similar prototype
Task<string> DoAsync() {
// Internally this method should call existing
// version of DoAsync method (see above)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您现有的 DoAsync 方法将异步运行。
在这种情况下,您可以这样包装它:
我看不到您现有的 DoAsync 方法如何报告异步错误。如有必要,您可以使用
TaskCompletionSource.TrySetException
报告异步错误。I'm assuming that your existing
DoAsync
method will run asynchronously.In that case, you can wrap it like this:
I don't see how your existing
DoAsync
method reports asynchronous errors. You can useTaskCompletionSource<T>.TrySetException
to report an asynchronous error if necessary.