难道/等待使用异步的创建一个新线程? [英] Does the use of async/await create a new thread?

查看:263
本文介绍了难道/等待使用异步的创建一个新线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的 TPL 我想知道:请问这是新的异步编程支持到C#5.0(通过新的异步等待关键字)涉及创建线程?

I am new to TPL and I am wondering: How does the asynchronous programming support that is new to C# 5.0 (via the new async and await keywords) relate to the creation of threads?

具体,并使用异步/的await 的创建一个新的线程,他们每次使用时?而如果使用有许多嵌套方法异步/的await ,是每一个这些方法创造了一个新的线程?

Specifically, does the use of async/await create a new thread each time that they are used? And if there many nested methods that use async/await, is a new thread created for each of those methods?

推荐答案

在短期

异步编程与异步和等待:线程

异步和等待关键字不会造成额外的线程是
  创建。异步方法不需要多线程因为异步
  方法并不在自己的线程中运行。该方法对当前运行
  同步上下文和使用时间的线程上,只有当
  方法是有效的。您可以使用Task.Run移动CPU绑定的工作提高到一个
  后台线程,而是后台线程不以过程中提供帮助
  这只是等待结果变得可用。

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available.

这篇关于难道/等待使用异步的创建一个新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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