本文档已存档,并且将不进行维护。

RunWorkerCompletedEventArgs.Result 属性

注意:此属性在 .NET Framework 2.0 版中是新增的。

获取表示异步操作结果的值。

命名空间:System.ComponentModel
程序集:System(在 system.dll 中)

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

public function get Result () : Object

属性值

表示异步操作结果的 Object

异常类型条件

TargetInvocationException

Error 不是 空引用(在 Visual Basic 中为 Nothing)。InnerException 属性持有对 Error 的引用。

InvalidOperationException

Cancelledtrue

在访问 Result 属性前,RunWorkerCompleted 事件处理程序应始终检查 ErrorCancelled 属性。如果已引发异常或已取消该操作,则访问 Result 属性将引发异常。

下面的代码示例演示如何使用 RunWorkerCompleted 事件处理异步操作的结果。此代码示例摘自一个为 BackgroundWorker 类提供的更大的示例。

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
显示: