Lingua

JoinableTaskFactory.SwitchToMainThreadAsync Metodo

Definizione

Overload

Nome Descrizione
SwitchToMainThreadAsync(CancellationToken)

Ottiene un oggetto awaitable le cui continuazioni vengono eseguite sul thread principale, in modo da attenuare sia i deadlock che la reentrancy.

SwitchToMainThreadAsync(Boolean, CancellationToken)

Ottiene un oggetto awaitable le cui continuazioni vengono eseguite nel contesto di sincronizzazione con cui è stata inizializzata questa istanza, in modo da attenuare sia i deadlock che la reentrancy.

SwitchToMainThreadAsync(CancellationToken)

Ottiene un oggetto awaitable le cui continuazioni vengono eseguite sul thread principale, in modo da attenuare sia i deadlock che la 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

Parametri

cancellationToken
CancellationToken

Token il cui annullamento pianifica immediatamente la continuazione in un thread del pool di thread e farà sì che la continuazione generi OperationCanceledException, anche se il chiamante è già presente nel thread principale.

Valori restituiti

Un oggetto awaitable.

Eccezioni

Viene restituito al chiamante in attesa se cancellationToken viene annullato, anche se il chiamante è già presente nel thread principale.

Commenti

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 il proprietario JoinableTaskContext viene creato con un nullSynchronizationContextoggetto , questo metodo non ha alcun effetto e il chiamante continuerà l'esecuzione sul thread originale.

Si applica a

SwitchToMainThreadAsync(Boolean, CancellationToken)

Ottiene un oggetto awaitable le cui continuazioni vengono eseguite nel contesto di sincronizzazione con cui è stata inizializzata questa istanza, in modo da attenuare sia i deadlock che la 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

Parametri

alwaysYield
Boolean

Valore che indica se il chiamante deve restituire anche se è già in esecuzione nel thread principale.

cancellationToken
CancellationToken

Token il cui annullamento pianifica immediatamente la continuazione in un thread del pool di thread e farà sì che la continuazione generi OperationCanceledException, anche se il chiamante è già presente nel thread principale.

Valori restituiti

Un oggetto awaitable.

Eccezioni

Viene restituito al chiamante in attesa se cancellationToken viene annullato, anche se il chiamante è già presente nel thread principale.

Commenti

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

Si applica a