Editar

Partilhar via


Task.Yield Method

Definition

Creates an awaitable task that asynchronously yields back to the current context when awaited.

public:
 static System::Runtime::CompilerServices::YieldAwaitable Yield();
public static System.Runtime.CompilerServices.YieldAwaitable Yield ();
static member Yield : unit -> System.Runtime.CompilerServices.YieldAwaitable
Public Shared Function Yield () As YieldAwaitable

Returns

A context that, when awaited, will asynchronously transition back into the current context at the time of the await. If the current SynchronizationContext is non-null, it is treated as the current context. Otherwise, the task scheduler that is associated with the currently executing task is treated as the current context.

Remarks

You can use await Task.Yield(); in an asynchronous method to force the method to complete asynchronously. If there is a current synchronization context (SynchronizationContext object), this will post the remainder of the method's execution back to that context. However, the context will decide how to prioritize this work relative to other work that may be pending. The synchronization context that is present on a UI thread in most UI environments will often prioritize work posted to the context higher than input and rendering work. For this reason, do not rely on await Task.Yield(); to keep a UI responsive. For more information, see the entry Useful Abstractions Enabled with ContinueWith in the Parallel Programming with .NET blog.

Applies to