【发布时间】:2014-05-26 05:13:55
【问题描述】:
我正在编写一些代码来执行一些网络请求、处理答案并将结果返回给调用者。在我看来,异步/等待的自然背景。 这是我写的方法:
protected async Task<ProcessingResult> ProcessWebPagesAsync(/* args */)
{
// awaits and other code here
}
调用者在专门为运行这些请求而创建的线程上运行,我目前无法更改实现。 我的问题是:
- 在不是来自线程池的线程上使用 async/await 是否有优势?
- 如何从现有代码的上下文中调用我的根方法? (见示例)
public class MyProcessor : ProcessorBase
{
public override ProcessingResult ProcessWebPages(/* args */)
{
return this.ProcessWebPagesAsync(/* args */).Result;
}
protected async Task<ProcessingResult> ProcessWebPages(/* args */)
{
// awaits and other code here
}
}
因此,ProcessorBase.ProcessWebPages() 在“专用”线程上被调用。
在这里使用 async/await 真的有意义吗?我有福利吗?
【问题讨论】:
标签: c# .net multithreading asynchronous async-await