سؤال

As the question says. I'd like to know if simply calling await in code has the potential to create a new thread.

Let's assume it's during a console app.

هل كانت مفيدة؟

المحلول

Microsoft's .NET 4.5 Task implementation will first try to invoke await-based continuations inline (i.e., on whatever thread completes the task). If it can't be executed inline (e.g., due to stack depth or use of a SynchronizationContext) the system thread pool is used instead. It won't explicitly create a new thread, though it might trigger the creation of a new thread in the pool.

See System.Threading.Tasks.AwaitTaskContinuation in the reference sources for details.

Note that this answer only applies to an awaited Task, and only on the Microsoft .NET 4.5 Framework. Also, if you are awaiting from a thread with a SynchronizationContext, and you are not using ConfigureAwait(false), then it would depend on the SynchronizationContext implementation. No new threads should be created for a WinForms, WPF, or Silverlight SynchronizationContext.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top