为什么没有的await异步函数产生一个编译器警告? [英] Why does an async function without an await result in a compiler warning?

查看:225
本文介绍了为什么没有的await异步函数产生一个编译器警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么异步功能在C#5都要求有至少1的await?我无法找到一个明确的理由/交代。

Can anyone explain why async functions in c# 5 are required to have at least 1 await? I can't find a clear reason/explaination.

这是必需的,我的意思是编译器警告当一个异步函数没有任何等待它的内部调用,但不会引发编译错误。

By required, I mean that the compiler warns when an async function doesn't have any await calls inside of it, but doesn't throw a compile error.

答案:

类似地,方法标记为异步必须至少有一个AWAIT。上的await,运行时将保存当前线程的状态和调用堆栈,使异步调用,身心恢复运行的消息循环来处理下一个消息,并保持该应用程序响应。当异步操作完成时,在下一调度机会,调用堆栈向上异步操作中被推回,并继续仿佛该呼叫是同步的。

Similarly, a method marked as async must have at least one await. On an await, the runtime will save the current thread's state and call stack, make the asynchronous call, and unwind back to the runtime's message loop to handle the next message and keep the app responsive. When the asynchronous operation is complete, at the next scheduling opportunity, the call stack to up the async operation is pushed back in and continued as if the call was synchronous.

不过从MSDN:

如果该方法不包含AWAIT前pression或声明,则同步执行。编译器警告提醒您不包含任何异步方法等待,因为这情况可能表示错误。

If the method does not contain an await expression or statement, then it executes synchronously. A compiler warning alerts you to any async methods that don't contain await because that situation might indicate an error.

什么类型的错误发生,值得这是一个编译器警告而不只是推荐的用法?

What type of error occur that merits this being a compiler warning versus just recommended usage?

推荐答案

MSDN有此警告一个很好的说明:的编译器警告(等级1)CS4014 。从它良好的报价是:

MSDN has a good description for this warning: Compiler Warning (level 1) CS4014. The good quote from it will be:

在大多数情况下,这种行为是不是你所期望的。

In most cases, that behavior isn't what you expect.

我觉得为什么这个警告存在主要原因是异步/的await是不是真的明显,开发商通常不喜欢我的失误将在下面介绍。

I think that the main reason why this warning exists is that async/await is not really obvious, and developers usually do mistakes like I will describe below.

例如你有一个方法,做一些沉重了几秒钟:

For example you have a method, which do something heavy for several seconds:

public int DoSomething()
{
    int sum = 0;

    for (int i = 0; i < 10; i++)
    {
        sum += i;
        Thread.Sleep(1000);
    }

    return sum;
}

您听说过异步等待地方,你想尝试一下。这是人们经常认为他们(不经常也许,但我认为这是它是如何工作之前,我阅读更多的文档,所以我们可以指望至少我)都移动到背景:

You heard about async and await somewhere and you want to try them. And this is how very often people think that they will move everything to background (maybe not often, but I thought that this is how it works before I read more documentation, so we can count at least me):

public async Task<int> DoSomething()
{
    int sum = 0;

    for (int i = 0; i < 10; i++)
    {
        sum += i;
        Thread.Sleep(1000);
    }

    return sum;
}

您觉得这个问题是解决了,但警告,告诉你,没有等待你不应该使用异步,因为这只是什么都不做,你的所有code将同步运行,以的最后一个样本异步类似于下一个code:

You think that problem is solved, but warning tells you that without await you should not use async, because it just does nothing, all your code will run synchronously, the last sample with async is similar to next code:

public Task<int> DoSomething()
{
    int sum = 0;

    for (int i = 0; i < 10; i++)
    {
        sum += i;
        Thread.Sleep(1000);
    }

    return Task.Result<int>(sum);
}

如果你做同样的背景下,调用该方法的一切。

Where you do everything on the same context, which calls this method.

所以这个警告的主要原因,我认为让人们知道,也许他们使用异步错了,这是阅读文档的时候。

So the main reason of this warning I think to let people know that probably they use async wrong and this is a time to read documentation.

这篇关于为什么没有的await异步函数产生一个编译器警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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