什么线程运行`await`关键字后面的code? [英] What thread runs the code after the `await` keyword?

查看:105
本文介绍了什么线程运行`await`关键字后面的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我发布一个简单的例子:

Let me just post a simple example:

    private void MyMethod()
    {
        Task task = MyAsyncMethod();
        task.Wait();
    }

    private async Task MyAsyncMethod()
    {
        //Code before await
        await MyOtherAsyncMethod();
        //Code after await
    }

比方说,我在运行单线程应用程序般的控制台APP-上述code。我有困难的时候如何理解code // code等待后将能够运行。




我明白,当我打的等待 MyAsyncMethod()控制关键字可追溯到的MyMethod (),但后来我与锁定螺纹task.Wait()。如果线程被锁定,怎么能 // code后等待永远运行,如果是应该把它的线程被锁定?




是否一个新的线程获得创建运行 // code等待后?还是主线程奇迹般地走出的 task.Wait()运行等待 // code后




我不知道这是如何工作的?

Let's say I run the above code in a single threaded app -like a console app-. I'm having a difficult time understanding how the code //Code after await would be able to run.

I understand that when I hit the await keyword in MyAsyncMethod() control goes back to MyMethod(), but then I'm locking the thread with task.Wait(). If the thread is locked, how can //Code after await ever run if the thread that is supposed to take it is locked?

Does a new thread get created to run //Code after await? Or does the main thread magically steps out of task.Wait() to run //Code after await?

I'm not sure how this is supposed to work?

推荐答案

code在winform应用程序发布将死锁,如果因为你阻挡与等待()。

Code as posted will "Deadlock" in Winform App if called from main thread because you're blocking the main thread with the Wait().

但在控制台应用程序工作的。但如何?

But in console app this works. but how?

答案已被隐藏在 SynchronizationContext.Current 的await 捕获的SynchronizationContext,当任务完成后,将继续在同一个的SynchronizationContext。

Answer is hidden in the SynchronizationContext.Current. await captures the "SynchronizationContext" and when the task is completed it will continue in the same "SynchronizationContext".

在winform应用程序 SynchronizationContext.Current 将被设置为 WindowsFormsSynchronizationContext 将张贴到调用消息循环,但谁去处理呢?出主线程在等待等待()

In winform app SynchronizationContext.Current will be set to WindowsFormsSynchronizationContext which will post to the call to "Message loop", but who is going to process that? out main thread is waiting in Wait().

在控制台应用程序 SynchronizationContext.Current 将不会被默认设定所以这将是时,没有的SynchronizationContext 可供的await捕捉到,因此将安排在延续线程池(TaskScheduler.Default这是ThreadpoolTask​​Scheduler)等等code等候作品(通过线程池线程)后,

In console app SynchronizationContext.Current will not be set by default so it will be null when no "SynchronizationContext" available for await to capture so it will schedule the continuation to ThreadPool(TaskScheduler.Default which is ThreadpoolTaskScheduler) and so the code after await works(through threadpool thread).

上述捕捉行为可以使用控制 Task.ConfigureAwait(假); 将美元的死锁,但p $ pvent winform应用程序code后等待不再在UI线程中运行。

Aforementioned capturing behavior can be controlled using Task.ConfigureAwait(false); which will prevent winform app from deadlocking but code after await no longer runs in UI thread.

这篇关于什么线程运行`await`关键字后面的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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