共用方式為


JoinableTaskFactory.SwitchToMainThreadAsync Method

Definition

Overloads

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)

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)

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 = default);
member this.SwitchToMainThreadAsync : System.Threading.CancellationToken -> Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable
Public Function SwitchToMainThreadAsync (Optional cancellationToken As CancellationToken = Nothing) As JoinableTaskFactory.MainThreadAwaitable

Parameters

cancellationToken
CancellationToken

A token whose cancellation will immediately schedule the continuation on a threadpool thread and will cause the continuation to throw OperationCanceledException, even if the caller is already on the main thread.

Returns

An awaitable.

Exceptions

Thrown back at the awaiting caller if cancellationToken is canceled, even if the caller is already on the main thread.

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

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 = default);
member this.SwitchToMainThreadAsync : bool * System.Threading.CancellationToken -> Microsoft.VisualStudio.Threading.JoinableTaskFactory.MainThreadAwaitable
Public Function SwitchToMainThreadAsync (alwaysYield As Boolean, Optional cancellationToken As CancellationToken = Nothing) As JoinableTaskFactory.MainThreadAwaitable

Parameters

alwaysYield
Boolean

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

cancellationToken
CancellationToken

A token whose cancellation will immediately schedule the continuation on a threadpool thread and will cause the continuation to throw OperationCanceledException, even if the caller is already on the main thread.

Returns

An awaitable.

Exceptions

Thrown back at the awaiting caller if cancellationToken is canceled, even if the caller is already on the main thread.

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