Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RunWorkerCompletedEventArgs.Result Property

Gets a value that represents the result of an asynchronous operation.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

public Object Result { get; }
/** @property */
public Object get_Result ()

public function get Result () : Object

Not applicable.

Property Value

An Object representing the result of an asynchronous operation.

Exception typeCondition

TargetInvocationException

Error is not a null reference (Nothing in Visual Basic). The InnerException property holds a reference to Error.

InvalidOperationException

Cancelled is true.

Your RunWorkerCompleted event handler should always check the Error and Cancelled properties before accessing the Result property. If an exception was raised or if the operation was canceled, accessing the Result property raises an exception.

The following code example demonstrates the use of the RunWorkerCompleted event to handle the result of an asynchronous operation. This code example is part of a larger example provided for the BackgroundWorker class.

// 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;
}

// 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.get_Error() != null) {
    
        MessageBox.Show(e.get_Error().get_Message());
    }
    else {
    
        if (e.get_Cancelled()) {
        
            // Next, handle the case where the user cancelled 
            // 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.set_Text("Cancelled");
        }
        else {
        
            // Finally, handle the case where the operation 
            // succeeded.
            resultLabel.set_Text(e.get_Result().ToString());
        }
    }

    // Enable the UpDown control.
    this.numericUpDown1.set_Enabled(true);

    // Enable the Start button.
    startAsyncButton.set_Enabled(true);

    // Disable the Cancel button.
    cancelAsyncButton.set_Enabled(false);
} //backgroundWorker1_RunWorkerCompleted

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2014 Microsoft