如何以及何时使用“异步”和“等待” [英] How and when to use ‘async’ and ‘await’

查看:85
本文介绍了如何以及何时使用“异步”和“等待”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解, async的主要事情之一 await 的目的是使代码易于编写和阅读-但使用它们等同于生成后台线程来执行较长的持续时间

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic?

我目前正在尝试最基本的示例。我在行中添加了一些评论。

I'm currently trying out the most basic example. I've added some comments inline. Can you clarify it for me?

// I don't understand why this method must be marked as `async`.
private async void button1_Click(object sender, EventArgs e)
{
    Task<int> access = DoSomethingAsync();
    // task independent stuff here

    // this line is reached after the 5 seconds sleep from 
    // DoSomethingAsync() method. Shouldn't it be reached immediately? 
    int a = 1; 

    // from my understanding the waiting should be done here.
    int x = await access; 
}

async Task<int> DoSomethingAsync()
{
    // is this executed on a background thread?
    System.Threading.Thread.Sleep(5000);
    return 1;
}


推荐答案

使用<$ c $时c> async 和 await ,编译器会在后台生成状态机。

When using async and await the compiler generates a state machine in the background.

这是一个示例,希望我可以解释正在发生的一些高级细节:

Here's an example on which I hope I can explain some of the high-level details that are going on:

public async Task MyMethodAsync()
{
    Task<int> longRunningTask = LongRunningOperationAsync();
    // independent work which doesn't need the result of LongRunningOperationAsync can be done here

    //and now we call await on the task 
    int result = await longRunningTask;
    //use the result 
    Console.WriteLine(result);
}

public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation 
{
    await Task.Delay(1000); // 1 second delay
    return 1;
}

好,那么这里会发生什么:

OK, so what happens here:


  1. Task< int> longRunningTask = LongRunningOperationAsync(); 开始执行 LongRunningOperation

独立工作是让我们假设主线程(线程ID = 1)完成操作,然后到达 await longRunningTask

Independent work is done on let's assume the Main Thread (Thread ID = 1) then await longRunningTask is reached.

现在,如果 longRunningTask 尚未完成并且仍在运行,则 MyMethodAsync ()将返回其调用方法,因此主线程不会被阻塞。完成 longRunningTask 之后,来自ThreadPool的线程(可以是任何线程)将返回到 MyMethodAsync()其先前的上下文并继续执行(在这种情况下,将结果打印到控制台)。

Now, if the longRunningTask hasn't finished and it is still running, MyMethodAsync() will return to its calling method, thus the main thread doesn't get blocked. When the longRunningTask is done then a thread from the ThreadPool (can be any thread) will return to MyMethodAsync() in its previous context and continue execution (in this case printing the result to the console).

第二种情况是 longRunningTask 已经完成执行,结果可用。当到达 await longRunningTask 时,我们已经有了结果,因此代码将继续在同一线程上执行。 (在这种情况下,将结果打印到控制台)。当然,上面的示例并非如此,其中涉及到 Task.Delay(1000)

A second case would be that the longRunningTask has already finished its execution and the result is available. When reaching the await longRunningTask we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for the above example, where there's a Task.Delay(1000) involved.

这篇关于如何以及何时使用“异步”和“等待”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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