backgroundworker BackgroundWorker.RunWorkerCompleted事件定义及相关重要说明

网安智编 厦门萤点网络科技 2025-07-15 00:01 91 0
. 事件定义 命名空间: . 程序集: .dll, ...dll 程序集: ...dll 程序集: .dll 程序集: .dll : .cs : .cs : .cs 重要 一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对...

. 事件定义

命名空间:

.

程序集:

.dll, ...dll

程序集:

...dll

程序集:

.dll

程序集:

.dll

:

.cs

:

.cs

:

.cs

重要

一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息, 不作任何明示或暗示的担保。

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

public:
 event System::ComponentModel::RunWorkerCompletedEventHandler ^ RunWorkerCompleted;

public event System.ComponentModel.RunWorkerCompletedEventHandler RunWorkerCompleted;

public event System.ComponentModel.RunWorkerCompletedEventHandler? RunWorkerCompleted;

member this.RunWorkerCompleted : System.ComponentModel.RunWorkerCompletedEventHandler 

Public Custom Event RunWorkerCompleted As RunWorkerCompletedEventHandler 

事件类型

示例

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

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

BackgroundWorker.RunWorkerCompleted事件处理 _backgroundworker_ System.ComponentModel命名空间

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

注解

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

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

Error的 .. 属性指示操作引发了异常。

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

注意

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

在访问 . 属性之前,rgs.Error事件处理程序应始终检查 和 rgs. 属性。 如果引发异常或取消操作,则 . 访问 属性将引发异常。

适用于另请参阅