Linguagem

JoinableTaskFactory.SwitchToMainThreadAsync Método

Definição

Sobrecargas

Nome Description
SwitchToMainThreadAsync(CancellationToken)

Obtém um awaitable cujas continuações são executadas no thread principal, de modo a atenuar deadlocks e reentração.

SwitchToMainThreadAsync(Boolean, CancellationToken)

Obtém um awaitable cujas continuações são executadas no contexto de sincronização com o qual essa instância foi inicializada, de modo a atenuar os deadlocks e a reentração.

SwitchToMainThreadAsync(CancellationToken)

Obtém um awaitable cujas continuações são executadas no thread principal, de modo a atenuar deadlocks e reentração.

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

Parâmetros

cancellationToken
CancellationToken

Um token cujo cancelamento agendará imediatamente a continuação em um thread de thread de threadpool e fará com que a continuação seja gerada OperationCanceledException, mesmo que o chamador já esteja no thread principal.

Retornos

Um aguardado.

Exceções

Jogado novamente no chamador aguardando se cancellationToken for cancelado, mesmo que o chamador já esteja no thread principal.

Comentários

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

Quando a propriedade JoinableTaskContext é criada com um nullSynchronizationContext, esse método não tem efeito e o chamador continuará a execução em seu thread original.

Aplica-se a

SwitchToMainThreadAsync(Boolean, CancellationToken)

Obtém um awaitable cujas continuações são executadas no contexto de sincronização com o qual essa instância foi inicializada, de modo a atenuar os deadlocks e a reentração.

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

Parâmetros

alwaysYield
Boolean

Um valor que indica se o chamador deve produzir mesmo que já esteja em execução no thread principal.

cancellationToken
CancellationToken

Um token cujo cancelamento agendará imediatamente a continuação em um thread de thread de threadpool e fará com que a continuação seja gerada OperationCanceledException, mesmo que o chamador já esteja no thread principal.

Retornos

Um aguardado.

Exceções

Jogado novamente no chamador aguardando se cancellationToken for cancelado, mesmo que o chamador já esteja no thread principal.

Comentários

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

Aplica-se a