AsyncLazy<T>.SuppressRelevance Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Contrassegna il codice seguente come irrilevante per la factory del valore ricevente AsyncLazy<T> .
public Microsoft.VisualStudio.Threading.AsyncLazy<T>.RevertRelevance SuppressRelevance();
member this.SuppressRelevance : unit -> Microsoft.VisualStudio.Threading.AsyncLazy<'T>.RevertRelevance
Public Function SuppressRelevance () As AsyncLazy(Of T).RevertRelevance
Valori restituiti
Valore da eliminare per ripristinare la pertinenza nella factory del valore.
Commenti
In alcuni casi il lavoro asincrono può essere disattivato all'interno di una factory di valori. Quando la factory di valori non richiede il completamento di questo lavoro prima che la factory di valori possa essere completata, può essere utile usare questo metodo per contrassegnare tale codice come irrilevante per la factory di valori. In particolare, questo può essere necessario quando l'attività di disattivazione può effettivamente includere codice che può essere in attesa del completamento della factory del valore stesso. Una situazione di questo tipo causerebbe un'eccezione InvalidOperationException se GetValueAsync(CancellationToken) la factory di valori non è già stata completata, che può introdurre errori non determinstici nel programma.
Un using blocco intorno al codice disattivato può aiutare il programma a ottenere un comportamento affidabile, come illustrato di seguito.
class MyClass {
private readonly AsyncLazy<int> numberOfApples;
public MyClass() {
this.numberOfApples = new AsyncLazy<int>(async delegate {
// We have some fire-and-forget code to run.
// This is *not* relevant to the value factory, which is allowed to complete without waiting for this code to finish.
using (this.numberOfApples.SuppressRelevance()) {
this.FireOffNotificationsAsync();
}
// This code is relevant to the value factory, and must complete before the value factory can complete.
return await this.CountNumberOfApplesAsync();
});
}
public event EventHandler? ApplesCountingHasBegun;
public async Task<int> GetApplesCountAsync(CancellationToken cancellationToken) {
return await this.numberOfApples.GetValueAsync(cancellationToken);
}
private async Task<int> CountNumberOfApplesAsync() {
await Task.Delay(1000);
return 5;
}
private async Task FireOffNotificationsAsync() {
// This may call to 3rd party code, which may happen to call back into GetApplesCountAsync (and thus into our AsyncLazy instance),
// but such calls should *not* be interpreted as value factory reentrancy. They should just wait for the value factory to finish.
// We accomplish this by suppressing relevance of the value factory while this code runs (see the caller of this method above).
this.ApplesCountingHasBegun?.Invoke(this, EventArgs.Empty);
}
}
AsyncLazy<T> Se è stato creato con , JoinableTaskFactoryquesto metodo chiama SuppressRelevance() anche sull'oggetto Context associato a tale factory.