简体   繁体   English

为什么在等待它被认为是异步的同一行上调用一个方法?

[英]Why is invoking a method on the same line as awaiting it considered asynchronous?

I'm looking at the examples on http://www.dotnetperls.com/async to better understand async/await, but the following is confusing to me: 我正在查看http://www.dotnetperls.com/async上的示例以更好地理解async / await,但以下内容让我感到困惑:

I understand why the example below is considered asynchronous. 我理解为什么下面的例子被认为是异步的。 HandleFileAsync is invoked, the Console.WriteLine call is made, then we await the completion of task before proceeding. 调用HandleFileAsync ,调用Console.WriteLine ,然后在继续之前等待任务完成。

static async void ProcessDataAsync()
{
     // Start the HandleFile method.
     Task<int> task = HandleFileAsync("C:\\enable1.txt");

     // Control returns here before HandleFileAsync returns.
     // ... Prompt the user.
     Console.WriteLine("Please wait patiently " +
         "while I do something important.");

     // Wait for the HandleFile task to complete.
     // ... Display its results.
     int x = await task;
     Console.WriteLine("Count: " + x);
}

However in the following example, we await a call to Task.Run which runs an action: 但是在下面的示例中,我们等待Task.Run的调用,该调用运行一个动作:

static async void Example()
{
     // This method runs asynchronously.
     int t = await Task.Run(() => Allocate());
     Console.WriteLine("Compute: " + t);
}

So, if we are awaiting the completion of Task.Run here, what exactly is happening asynchronously? 那么,如果我们在等待 Task.Run的完成,那么异步发生了什么呢? I thought it becomes a blocking call once as soon as we await the subsequent task's execution to complete, which, in this case is invoked on the same line. 我们认为一旦我们等待后续任务的执行完成就会成为阻塞调用,在这种情况下,在同一行上调用它。

What am I missing? 我错过了什么?

I thought it becomes a blocking call once as soon as we await the subsequent task's execution to complete, which, in this case is invoked on the same line. 我们认为一旦我们等待后续任务的执行完成就会成为阻塞调用,在这种情况下,在同一行上调用它。 What am I missing? 我错过了什么?

Your belief is false; 你的信念是错误的; that's what you're missing. 这就是你所缺少的。 "await" means "return now, go run something else while we are asynchronously waiting , and when the result is available, come back here." “等待”的意思是“现在返回,在我们异步等待时运行别的东西,当结果可用时,回到这里。”

Fetching the result of the task does what you think await does. 获取任务的结果会做您认为等待的事情。 We would not have had to invent await if all it did was synchronously fetch the result of the task! 如果它只是同步获取任务的结果,我们就不必发明等待! It asynchronously fetches the result of the task. 异步获取任务的结果。

While we're at it, this comment is wrong: 虽然我们在这,但这个评论是错误的:

// Control returns here before HandleFileAsync returns.

How could that possibly be? 怎么可能呢? HandleFileAsync returned a task! HandleFileAsync返回了一个任务! How did control get to that point with a task in hand if HandleFileAsync did not return? 如果HandleFileAsync没有返回,控制如何通过手头的任务达到这一点? Of course it returned. 当然它回来了。

And this comment is misleading: 这条评论具有误导性:

// Wait for the HandleFile task to complete.

That should be asynchronously wait for the task to complete. 这应该是异步等待任务完成。 By asynchronously waiting, remember, we mean "return now, go run more work, and when the task is complete, resume at this point with the result in hand." 通过异步等待,记住,我们的意思是“现在返回,继续运行更多的工作,当任务完成时,在此时恢复结果。”

I would find a better tutorial if I were you. 如果我是你,我会找到更好的教程。

what exactly is happening asynchronously? 到底发生了什么异步?
consider this: 考虑一下:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static volatile uint i;
        static uint Test()
        {
            Thread.Sleep(1000);
            i = 2; //uint.MaxValue;
            while (i-- > 0) { }
            return i;
        }

        static bool done;
        static async void Example()
        {
            var t = await Task.Run(() => Test());
            Console.WriteLine("result: " + t);
            done = true;
        }
        static void Main(string[] args)
        {
            Example();
            Console.WriteLine("wait: Control returns here before Example() returns ");
            while (!done) { }
            Console.WriteLine("done ");
        }
    }
}

the Example(); Example(); itself is happening asynchronously. 本身就是异步发生的。 so if you remove while (!done) { } 所以,如果你删除while (!done) { }
the program exits before Example() finishes. 程序在Example()完成之前退出。
I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM