JoinableTaskFactory.SwitchToMainThreadAsync JoinableTaskFactory.SwitchToMainThreadAsync Method

Definition

Overloads

SwitchToMainThreadAsync(CancellationToken) SwitchToMainThreadAsync(CancellationToken)

Gets an awaitable whose continuations execute on the synchronization context that this instance was initialized with, in such a way as to mitigate both deadlocks and reentrancy.

SwitchToMainThreadAsync(Boolean, CancellationToken) SwitchToMainThreadAsync(Boolean, CancellationToken)

Gets an awaitable whose continuations execute on the synchronization context that this instance was initialized with, in such a way as to mitigate both deadlocks and reentrancy.

SwitchToMainThreadAsync(CancellationToken) SwitchToMainThreadAsync(CancellationToken)

Gets an awaitable whose continuations execute on the synchronization context that this instance was initialized with, in such a way as to mitigate both deadlocks and reentrancy.

public Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable SwitchToMainThreadAsync (System.Threading.CancellationToken cancellationToken = null);
member this.SwitchToMainThreadAsync : System.Threading.CancellationToken -> Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable

Parameters

cancellationToken
CancellationToken CancellationToken

A token whose cancellation will immediately schedule the continuation on a threadpool thread (if the transition to the main thread is not already complete). The token is ignored if the caller was already on the main thread.

Returns

Exceptions

Thrown back at the awaiting caller from a background thread when cancellationToken is canceled before any required transition to the main thread is complete. No exception is thrown if the caller was already on the main thread before calling this method, or if the main thread transition completes before the thread pool responds to cancellation.

Remarks


   private async Task SomeOperationAsync() {
       // on the caller's thread.
       await DoAsync();
  
       // Now switch to a threadpool thread explicitly.
       await TaskScheduler.Default;
  
       // Now switch to the Main thread to talk to some STA object.
       await this.JobContext.SwitchToMainThreadAsync();
       STAService.DoSomething();
   }

SwitchToMainThreadAsync(Boolean, CancellationToken) SwitchToMainThreadAsync(Boolean, CancellationToken)

Gets an awaitable whose continuations execute on the synchronization context that this instance was initialized with, in such a way as to mitigate both deadlocks and reentrancy.

public Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable SwitchToMainThreadAsync (bool alwaysYield, System.Threading.CancellationToken cancellationToken = null);
member this.SwitchToMainThreadAsync : bool * System.Threading.CancellationToken -> Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable

Parameters

alwaysYield
Boolean Boolean

A value indicating whether the caller should yield even if already executing on the main thread.

cancellationToken
CancellationToken CancellationToken

A token whose cancellation will immediately schedule the continuation on a threadpool thread.

Returns

Exceptions

Thrown back at the awaiting caller from a background thread when cancellationToken is canceled before any required transition to the main thread is complete. No exception is thrown if alwaysYield is false and the caller was already on the main thread before calling this method, or if the main thread transition completes before the thread pool responds to cancellation.

Remarks


   private async Task SomeOperationAsync()
   {
       // This first part can be on the caller's thread, whatever that is.
       DoSomething();
  
       // Now switch to the Main thread to talk to some STA object.
       // Supposing it is also important to *not* do this step on our caller's callstack,
       // be sure we yield even if we're on the UI thread.
       await this.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true);
       STAService.DoSomething();
   }

Applies to