简体   繁体   English

Task.Delay是如何工作的?

[英]How does Task.Delay work exactly?

They say Task.Delay() is an async Thread.Sleep(). 他们说Task.Delay()是一个异步的Thread.Sleep()。 To test this I wrote below code. 为了测试这个我写下面的代码。 I expect to print immediately "One" then 3 seconds later result variable (15) will be printed. 我希望立即打印“One”,然后3秒后打印结果变量(15)。 2 seconds after this, "Two" will be printed. 在此之后2秒,将打印“Two”。 But it doesnt seem so. 但它似乎并非如此。 "One" is not immediately printed. “One”不会立即打印出来。 "One" is printed 3 seconds later. 3秒后打印“One”。 Why does it wait 3 seconds to print "One" ? 为什么要等3秒钟打印“One”?

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

namespace ConsoleApplication31
{
class Program
{
    public static int Multiply(int a, int b)
    {
        return a * b;
    }

    public static async Task DoRunAsync(int m, int n)
    {
        int result = await Task.Run(() => Multiply(m, n));
        await Task.Delay(3000);
        Console.WriteLine("One");
        Console.WriteLine(result);
    }

    static void Main(string[] args)
    {
        Task t = DoRunAsync(3, 5);
        Thread.Sleep(5000);
        Console.WriteLine("Two");
    }
}
}

Modifying your DoRunAsync method as follows will make things work as intended: 如下修改DoRunAsync方法将使事情按预期工作:

public static async Task DoRunAsync(int m, int n)
{
    int result = await Task.Run(() => Multiply(m, n));
    Console.WriteLine("One"); // before the delay, not after...
    await Task.Delay(3000);
    Console.WriteLine(result);
}

Your code behaves as follows: 您的代码行为如下:

await Task.Delay(3000); // Await 3 seconds...
Console.WriteLine("One"); // Once you are done awaiting, print the string...

If you await before printing the string, you can't expect it to be printed immediately... 如果您打印字符串之前等待,则不能指望它立即打印...

It takes 3 seconds to print "One" because you await -ed Task.Delay too soon. 打印"One"需要3秒钟,因为你很快就await Task.Delay

Change the code as follows to get the result that you expected: 更改代码如下,以获得您期望的结果:

int result = await Task.Run(() => Multiply(m, n));
var taskDelay = Task.Delay(3000); // No blocking here, so
Console.WriteLine("One");                              // printing can proceed.
await taskDelay; // This causes a block for the remainder of 3 seconds
Console.WriteLine(result);

When you start the delay task prior to printing "One" without await -ing it, subsequent WriteLine could complete without a delay. 当您在打印"One"之前启动延迟任务而不await它时,后续的WriteLine可以在没有延迟的情况下完成。

The job of await is to suspend the current method until whatever awaitable you pass to it is complete . await的工作是暂停当前方法,直到您传递给它的任何等待完成为止。 How, why and when that awaitable was created and started are of no relevance to what the await keyword accomplishes. 创建和启动await ,原因和时间await关键字的完成无关。

I think you're under the impression that await starts something, when in fact it's the opposite - it marks a point in your code where you wait for something to finish . 我认为你的印象就是await 开始了,事实上它恰恰相反 - 它标志着你在等待某些事情完成的代码中的一个点。

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

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