一个void方法为什么要异步才能够等待? [英] Why should a void method be async to be able to await?

查看:787
本文介绍了一个void方法为什么要异步才能够等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有无效返回类型的方法,而我需要等待在此操作方法。

Assume I have a method that has void return type and that I need to await on an operation in this method.

public void someOperation()
{
    //dostuff
    var res = await someOtherOperation();
    //do some other stuff that needs res.
}

当我试图编译这个code我得到了 someOperation 的错误说必须要申报异步 。我不明白为什么。我理解为什么它会如果方法有返回值,而不是在这里时,它的无效。甚至在的情况下,当等待操作对方法的返回值没有影响。结果,
这已经被问道此内容问题,但我没有找到答案我一直在寻找对于。 这家伙仅仅提到:

When I tried to compile this code I got the error saying someOperation has to be declare async. I don't understand why. I understand why it would if the method had a return value, but not here when it's void. or even in the case when the awaiting operation has no effect on the return value of method.
This has already been asked as a part of this question but I didn't find the answer I was looking for. This guy merely mentions:

async关键字启用的await关键字。因此,使用任何方法的await
  必须标明异步。

The async keyword enables the await keyword. So any method using await must be marked async.


  • 首先,我不知道这意味着异步关键字允许的await 关键字。

  • 其次,重申我的问题,我不知道为什么它的要求

  • First of all, I'm not sure what it means that async keyword enables await keyword.
  • And secondly, restating my question, I'm not sure why it is required?
  • 推荐答案

    异步方法必须标记异步。在异步等待关键字配对在一起,主要是为了向后兼容的原因; 等待是一个有效的标识符(不是关键字),所以当它被添加到语言,他们还增加了异步关键字。

    Asynchronous methods must be marked async. The async and await keywords are paired together, primarily for backwards-compatibility reasons; await was a valid identifier (not a keyword), so when it was added to the language, they also added the async keyword.

    另外,他们的可能的已经走了一个多字的关键字为等待,如指日可待。然而,语言设计团队决定去与配对,因为它清晰而明确地标记所有异步法 - 这是为编译器和人类解析容易

    Alternatively, they could have gone with a multi-word keyword for await, such as await for. However, the language design team decided to go with the pairing, since it clearly and explicitly marks all async methods - it's easier for both compilers and humans to parse.

    我对这个问题的博客文章,链接到埃里克利珀的权威博客文章以及在<一个讨论href=\"http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence-await.aspx\">blog评论, Channel9的论坛,<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/async/thread/75493675-4a39-4958-a493-ad8a96f8a19d\">MSDN论坛,当然就在这里堆栈溢出的。

    I have a blog post on the subject, with links to Eric Lippert's definitive blog post as well as discussions in blog comments, Channel9 forums, MSDN forums, and of course right here on Stack Overflow.

    注意的自然的一个的返回类型异步无结果值的方法是工作,不是无效无效异步方法不自然的返回类型。所以,在看到等待操作者必须是异步方法中的当你默认的反应误差应该将其标记异步的改变从返回类型无效任务

    Note that the natural return type of an async method without a result value is Task, not void. void is an unnatural return type for async methods. So your default reaction when seeing the "await operator must be within an async method" error should be to mark it async and change the return type from void to Task:

    public async Task someOperationAsync()
    {
      //dostuff
      var res = await someOtherOperationAsync();
      //do some other stuff that needs res.
    }
    

    这是继在我的关于这个问题 MSDN文章之一:避免异步无效异步无效中的允许的由语言设计者事件处理程序(或code是的逻辑的事件处理程序像 ICommand.Execute );任何其他方式使用会导致问题。

    This follows one of the best practices in my MSDN article on the subject: avoid async void. async void was allowed by the language designers for event handlers (or code that is logically an event handler, like ICommand.Execute); any other usage is going to cause problems.

    这篇关于一个void方法为什么要异步才能够等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆