.NET Framework 类库
BackgroundWorker..::.RunWorkerCompleted 事件

更新:2007 年 11 月

当后台操作已完成、被取消或引发异常时发生。

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

语法

Visual Basic(声明)
Public Event RunWorkerCompleted As RunWorkerCompletedEventHandler
Visual Basic (用法)
Dim instance As BackgroundWorker
Dim handler As RunWorkerCompletedEventHandler

AddHandler instance.RunWorkerCompleted, handler
C#
public event RunWorkerCompletedEventHandler RunWorkerCompleted
Visual C++
public:
 event RunWorkerCompletedEventHandler^ RunWorkerCompleted {
    void add (RunWorkerCompletedEventHandler^ value);
    void remove (RunWorkerCompletedEventHandler^ value);
}
J#
/** @event */
public void add_RunWorkerCompleted (RunWorkerCompletedEventHandler value)
/** @event */
public void remove_RunWorkerCompleted (RunWorkerCompletedEventHandler value)
JScript
JScript 不支持事件。
备注

DoWork 事件处理程序返回时将引发此事件。

如果操作成功完成,并且其结果在 DoWork 事件处理程序中进行了分配,则可以通过 RunWorkerCompletedEventArgs..::.Result 属性访问该结果。

System.ComponentModel..::.RunWorkerCompletedEventArgsError 属性指示此操作已引发异常。

System.ComponentModel..::.RunWorkerCompletedEventArgsCancelled 属性指示后台操作是否已处理取消请求。如果通过检查 CancellationPending 标志并将 System.ComponentModel..::.DoWorkEventArgsCancel 标志设置为 trueDoWork 事件处理程序中的代码检测到取消请求,则 System.ComponentModel..::.RunWorkerCompletedEventArgsCancelled 标志也会设置为 true

警告:

请注意,DoWork 事件处理程序中的代码有可能在发出取消请求时完成其工作,轮询循环可能会错过设置为 trueCancellationPending。在这种情况下,即使发出了取消请求,RunWorkerCompleted 事件处理程序中 System.ComponentModel..::.RunWorkerCompletedEventArgsCancelled 标志也不会设置为 true。这种情况被称作争用状态,它是多线程编程中的常见问题。有关多线程设计问题的更多信息,请参见 托管线程处理的最佳做法

RunWorkerCompleted 事件处理程序应当总是先检查 AsyncCompletedEventArgs..::.ErrorAsyncCompletedEventArgs..::.Cancelled 属性,然后再访问 RunWorkerCompletedEventArgs..::.Result 属性。如果引发了异常或者该操作已取消,则访问 RunWorkerCompletedEventArgs..::.Result 属性将引发异常。

示例

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

Visual Basic
' 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 'backgroundWorker1_RunWorkerCompleted
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;
}
Visual C++
// 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;
}
J#
// 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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

版本信息

.NET Framework

受以下版本支持:3.5、3.0、2.0
另请参见

参考

其他资源

标记 :


Page view tracker