如果可能的话,我应该省略 async/await 吗?
我有一个需要很长时间才能运行的方法:它调用数据库并同步进行某些计算:
public static MyResult MyMethod(int param1, int param2)
{
// run a DB query, wait for result, make calculations...
...
}
我想为其编写一个包装器,以便能够通过“await”关键字从我的 WinForms UI 中使用它。为此,我创建了另一个方法 MyResultAsync。我有一个选择,具体如何写:
// option 1
public async static Task<MyResult> MyResultAsync(int param1, int param2)
{
return await TaskEx.Run(() => MyMethod(param1, param2));
}
// option 2
public static Task<MyResult> MyResultAsync(int param1, int param2)
{
return TaskEx.Run(() => MyMethod(param1, param2));
}
那么,哪个选项更可取,为什么?正如您所看到的,区别只是存在/不存在“async”和“await”关键字。
谢谢你!
I have a method which takes long to run: it calls the DB and makes certain calculations synchronously:
public static MyResult MyMethod(int param1, int param2)
{
// run a DB query, wait for result, make calculations...
...
}
I want to write a wrapper for it, to be able to use it from my WinForms UI with 'await' keyword. To do this, I create another method, MyResultAsync. I have a choice, how exactly to write it:
// option 1
public async static Task<MyResult> MyResultAsync(int param1, int param2)
{
return await TaskEx.Run(() => MyMethod(param1, param2));
}
// option 2
public static Task<MyResult> MyResultAsync(int param1, int param2)
{
return TaskEx.Run(() => MyMethod(param1, param2));
}
So, which option is preferable and why? As you can see, the difference is just in presence/absence of 'async' and 'await' keywords.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用第二个选项。
您的第一个选项创建一个
Task
,然后创建另一个Task
来包装它。包装不会增加任何价值,只会增加开销。Stephen Toub 有一个来自 BUILD 的精彩视频,名为 异步之禅:最佳性能的最佳实践,其中涵盖了
async
/await
开销和替代方案。Use the second option.
Your first option creates a
Task<MyResult>
, and then creates anotherTask<MyResult>
to wrap it. The wrapping doesn't add any value, just overhead.Stephen Toub has a great video from BUILD called The Zen of Async: Best Practices for Best Performance, which covers the
async
/await
overhead and alternatives.这都是关于异步实际如何工作的。如果您阅读http://msdn.microsoft.com/en-us/magazine/ hh456402.aspx,特别是:http://blogs.msdn.com/b/pfxteam/archive /2011/10/24/10229662.aspx 您会发现 async 关键字使编译器在您的代码中构建一个状态机。这是开销,并且出于性能考虑,性能会更差,因为给定的代码中绝对不需要它。
至于可读性:我应该只在真正能产生影响的地方使用异步。对我来说,异步意味着某个方法以异步方式分多个步骤完成。当您定义像上面这样的简单代理方法时,您不想让语法进一步复杂化。
Its all about how async actually works. If you read http://msdn.microsoft.com/en-us/magazine/hh456402.aspx and in particular: http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229662.aspx you'll find that the async keyword makes the compiler build a statemachine within your code. This is overhead and for performance sake, will perform worse since it's absolutely not required in the code given.
As for readability: I should use async only where it really can make a difference. async for me means that a certain method finishes in multiple steps in an async manner. When you define a simple proxy method like the above, you dont want to complicate the syntax any further.