JoinableTaskFactory.SwitchToMainThreadAsync(CancellationToken) JoinableTaskFactory.SwitchToMainThreadAsync(CancellationToken) Method

Definition

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.

Returns

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();
   }

Applies to