RunWorkerCompletedEventArgs.Result Właściwość

Definicja

Pobiera wartość reprezentującą wynik operacji asynchronicznej.

C#
public object Result { get; }
C#
public object? Result { get; }

Wartość właściwości

Wynik Object operacji asynchronicznej.

Wyjątki

Error nie nulljest . Właściwość InnerException zawiera odwołanie do Errorelementu .

Przykłady

Poniższy przykład kodu pokazuje użycie RunWorkerCompleted zdarzenia do obsługi wyniku operacji asynchronicznej. Ten przykład kodu jest częścią większego przykładu podanego BackgroundWorker dla klasy.

C#
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
    object sender, RunWorkerCompletedEventArgs e)
{
    // First, handle the case where an exception was thrown.
    if (e.Error != null)
    {
        MessageBox.Show(e.Error.Message);
    }
    else if (e.Cancelled)
    {
        // Next, handle the case where the user canceled 
        // the operation.
        // Note that due to a race condition in 
        // the DoWork event handler, the Cancelled
        // flag may not have been set, even though
        // CancelAsync was called.
        resultLabel.Text = "Canceled";
    }
    else
    {
        // Finally, handle the case where the operation 
        // succeeded.
        resultLabel.Text = e.Result.ToString();
    }

    // Enable the UpDown control.
    this.numericUpDown1.Enabled = true;

    // Enable the Start button.
    startAsyncButton.Enabled = true;

    // Disable the Cancel button.
    cancelAsyncButton.Enabled = false;
}

Uwagi

Procedura RunWorkerCompleted obsługi zdarzeń powinna zawsze sprawdzać Error właściwości i Cancelled przed uzyskaniem Result dostępu do właściwości. Jeśli zgłoszono wyjątek lub jeśli operacja została anulowana, dostęp Result do właściwości zgłasza wyjątek.

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Zobacz też