如何编写简单的异步方法?
使用带有 async/await 关键字的最新 CTP5,我编写了一些代码,显然无法编译:
class Program
{
public class MyClass
{
async public Task<int> Test()
{
var result = await TaskEx.Run(() =>
{
Thread.Sleep(3000);
return 3;
});
return result;
}
}
static void Main(string[] args)
{
var myClass = new MyClass();
//The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error ??!!
int result = await myClass.Test();
Console.ReadLine();
}
}
“The 'await' 运算符只能在标记有 'async' 修饰符的方法或 lambda 错误中使用是什么原因?” (我选择了 Visual Studio 指向我的行)
Using latest CTP5 with async/await keywords, I wrote some code, which apparently cannot compile:
class Program
{
public class MyClass
{
async public Task<int> Test()
{
var result = await TaskEx.Run(() =>
{
Thread.Sleep(3000);
return 3;
});
return result;
}
}
static void Main(string[] args)
{
var myClass = new MyClass();
//The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error ??!!
int result = await myClass.Test();
Console.ReadLine();
}
}
What is th reason of "The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error?" (I've selected the line which Visual Studio point me to)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道是否可以将 Main 标记为异步,但是您需要在任何使用
await
的方法的声明中包含async
关键字。例如:I don't know if you can mark Main as async, but you need to include the
async
keyword in the declaration of any method that usesawait
. For example:await
与Wait()
不同;执行await
是对该方法的重大重写,尤其会影响调用者对该方法如何退出的期望。你是对的,除了告诉编译器启用某些功能(例如unsafe
、等开关)之外,它实际上并没有做太多事情(警告:返回类型)检查
和未检查
(如果您考虑一下)) - 但请考虑:在您的示例中,这实际上非常重要。如果Main()
退出(并且我们假设没有其他线程)-您的exe是toast。走了。不再存在。添加async
会让您认为仅仅因为方法退出并不意味着它已经完成。在准备好之前,您确实不希望Main()
退出。作为次要效果,此开关还正式规定该方法只能返回诸如
Task
; 之类的内容。如果没有开关,您可能会想稍后将其设为异步,这可能是一个重大的破坏性更改。await
is not the same asWait()
; doing anawait
is a significant re-writing of that method, and in particular affects the expectation of how that method exits to the caller. You are right in that it doesn't actually do much (caveat: return types) except tell the compiler to enable some things (as do switches likeunsafe
,checked
andunchecked
if you think about it) - but consider: this actually matters hugely in your example. IfMain()
exits (and we assume no other threads) - you exe is toast. Gone. No longer exists. Addingasync
makes you consider that just because the method exits doesn't mean it has finished. You really don't wantMain()
exiting before you are ready.As a secondary effect, this switch also formalises that the method can only return things like
Task
; without the switch, you might be tempted to make it async later, which could be a significantly breaking change.异步方法的返回类型可以为 void 或 Task。如果返回类型不是 void,调用者仍然可以在 Main 入口方法中使用 .Net 4 中引入的标准等待机制(不能标记为异步)。这是一个简单的例子:
An async method can have a return type of void or Task. If the return type is not void the caller can still use the standard Wait mechanism introduced in .Net 4 inside the Main entry method (which can not be marked async). Here's a simple example: