Sprache

JoinableTaskFactory.SwitchToMainThreadAsync Methode

Definition

Überlädt

Name Beschreibung
SwitchToMainThreadAsync(CancellationToken)

Ruft eine erwartbare, deren Fortsetzungen im Hauptthread ausgeführt werden, um sowohl Deadlocks als auch Reentrancy zu mindern.

SwitchToMainThreadAsync(Boolean, CancellationToken)

Ruft eine wartende Funktion ab, deren Fortsetzungen im Synchronisierungskontext ausgeführt werden, mit dem diese Instanz initialisiert wurde, um sowohl Deadlocks als auch Reentrancy zu mindern.

SwitchToMainThreadAsync(CancellationToken)

Ruft eine erwartbare, deren Fortsetzungen im Hauptthread ausgeführt werden, um sowohl Deadlocks als auch Reentrancy zu mindern.

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

Parameter

cancellationToken
CancellationToken

Ein Token, dessen Abbruch die Fortsetzung in einem Threadpoolthread sofort plant und dazu führt, dass die Fortsetzung ausgelöst OperationCanceledExceptionwird, auch wenn sich der Aufrufer bereits im Hauptthread befindet.

Gibt zurück

Eine wartebar.

Ausnahmen

Wird beim absagenden Aufrufer cancellationToken wieder ausgelöst, auch wenn sich der Aufrufer bereits im Hauptthread befindet.

Hinweise

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

Wenn das Besitzering JoinableTaskContext mit einer nullSynchronizationContexterstellt wird, hat diese Methode keine Auswirkung, und der Aufrufer setzt die Ausführung im ursprünglichen Thread fort.

Gilt für:

SwitchToMainThreadAsync(Boolean, CancellationToken)

Ruft eine wartende Funktion ab, deren Fortsetzungen im Synchronisierungskontext ausgeführt werden, mit dem diese Instanz initialisiert wurde, um sowohl Deadlocks als auch Reentrancy zu mindern.

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

Parameter

alwaysYield
Boolean

Ein Wert, der angibt, ob der Aufrufer auch dann zurückgegeben werden soll, wenn er bereits im Hauptthread ausgeführt wird.

cancellationToken
CancellationToken

Ein Token, dessen Abbruch die Fortsetzung in einem Threadpoolthread sofort plant und dazu führt, dass die Fortsetzung ausgelöst OperationCanceledExceptionwird, auch wenn sich der Aufrufer bereits im Hauptthread befindet.

Gibt zurück

Eine wartebar.

Ausnahmen

Wird beim absagenden Aufrufer cancellationToken wieder ausgelöst, auch wenn sich der Aufrufer bereits im Hauptthread befindet.

Hinweise

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

Gilt für: