在转移到与调用方相同的线程之前(在UI应用程序中),是否等待内部创建另一个线程 [英] Does await create another thread internally before shifting to the same thread as the caller (for UI application)

查看:83
本文介绍了在转移到与调用方相同的线程之前(在UI应用程序中),是否等待内部创建另一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对async/await的了解是,当任务完成时,继续在调用await的同一上下文中运行,在我的情况下,这将是UI线程. 但是我的问题是,它会在IO完成之后并在移至相同的UI线程之前(内部)创建一个新线程.

What I know about async/await is that when the task completes, the continuation is run on the same context when the await was called, which would, in my case, be the UI thread. But my question is, does it create a new thread (internally) after IO is complete and before moving to the same UI thread.

我正在共享一段代码.如果单击一次此按钮,则表明执行等待之前可用线程为1023,但此后,可用线程降至1022.尽管当我检查线程ID时,它与UI线程相同.

I am sharing a piece of code. If I click on this button once, It shows that available thread is 1023 before executing await, but after that, available threads dropped to 1022. Although When I check the thread id, it is the same as UI thread.

    private async void button1_ClickAsync(object sender, EventArgs e)
    {
        int x, y;
        ThreadPool.GetAvailableThreads(out x, out y);
        textBox1.Text = x.ToString()+"..."+y.ToString();
        await Task.Delay(5000);
        ThreadPool.GetAvailableThreads(out x, out y);
        textBox1.Text = x.ToString() + "..." + y.ToString();
    }

但是有趣的是,下次我单击此按钮时,可用线程数仍为1023(在等待之前和之后).

But interestingly, next time when I click on this button, number of available threads remains 1023 (before and after await).

推荐答案

但是我的问题是,它会在IO完成之后并移至相同的UI线程之前(内部)创建一个新线程.

But my question is, does it create a new thread (internally) after IO is complete and before moving to the same UI thread.

其他线程可能会暂时使用,但是您不必为此担心.

Other threads may be temporarily used, but you don't need to worry about that.

尤其是,.NET上的I/O通常通过作为线程池一部分的I/O完成端口进行访问. I/O线程会根据需要自动添加和删除.通常,在I/O实际准备好返回到代码之前(例如,解析HTTP响应标头),还有一些其他工作要做,因此BCL I/O代码的 lot 实际上使用I/O线程只是为了将工作排队到线程池中.因此,I/O代码经常(简短地)使用线程池工作线程.

In particular, I/O on .NET generally goes through an I/O completion port that is part of the thread pool. I/O threads are automatically added and removed as necessary. Fairly often, the I/O has some additional work to do before it actually is ready to return to your code (e.g., parsing HTTP response headers), so a lot of the BCL I/O code will actually use the I/O thread just to queue work to the thread pool. So a thread pool worker thread is often used (briefly) by I/O code.

此外,在这个特定的示例中,我认为还有一个单独的计时器线程可以合并系统计时器.当然,这是一个实现细节,并且随时可能更改.

Also, in this particular example, I believe there's a separate timer thread as well, that coalesces system timers. Naturally, this is an implementation detail and subject to change.

因此,总而言之,可能会创建/销毁/临时使用其他线程,但是您不必担心.它们全部由BCL或.NET运行时以非常有效的方式进行管理,在重用线程(最大程度地减少用户流失)和最小化资源使用(特别是内存)之间取得了平衡.

So, in summary, other threads may be created/destroyed/temporarily used, but you don't have to worry about it. They're all managed by the BCL or .NET runtime in a very efficient manner, striking a balance between reusing threads (minimizing churn) and minimizing resource use (especially memory).

这篇关于在转移到与调用方相同的线程之前(在UI应用程序中),是否等待内部创建另一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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