RunWorkerCompletedEventArgs.Result 属性     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取表示异步操作结果的值。
public:
 property System::Object ^ Result { System::Object ^ get(); };public object Result { get; }public object? Result { get; }member this.Result : objPublic ReadOnly Property Result As Object属性值
表示异步操作结果的 Object。
例外
              Error 不是 null。 
              InnerException 属性持有对 Error 的引用。
              Cancelled 为 true。
示例
下面的代码示例演示如何使用 RunWorkerCompleted 事件来处理异步操作的结果。 此代码示例是为 BackgroundWorker 类提供的一个更大示例的一部分。
// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted( Object^ /*sender*/, RunWorkerCompletedEventArgs^ e )
{
   // First, handle the case where an exception was thrown.
   if ( e->Error != nullptr )
   {
      MessageBox::Show( e->Error->Message );
   }
   else
   if ( e->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->Text = "Cancelled";
   }
   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.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 Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
    ' First, handle the case where an exception was thrown.
    If (e.Error IsNot Nothing) Then
        MessageBox.Show(e.Error.Message)
    ElseIf e.Cancelled Then
        ' 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()
    End If
    ' Enable the UpDown control.
    Me.numericUpDown1.Enabled = True
    ' Enable the Start button.
    startAsyncButton.Enabled = True
    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
End Sub
注解
在RunWorkerCompleted访问 Result 属性之前,Error事件处理程序应始终检查 和 Cancelled 属性。 如果引发异常或取消操作,则访问 Result 属性将引发异常。