RunWorkerCompletedEventArgs 类

定义

为 MethodName 事件提供数据CompletedProvides data for the MethodNameCompleted event.

public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
继承
RunWorkerCompletedEventArgs

示例

下面的代码示例演示如何使用 RunWorkerCompletedEventArgsThe following code example illustrates the use of RunWorkerCompletedEventArgs. 此示例是 BackgroundWorker 类的更大示例的一部分。This example is part of a larger sample for the BackgroundWorker class.

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

注解

使用异步操作的基于事件的异步模式时,Windows 窗体的窗体或控件通过调用 BackgroundWorker.RunWorkerAsync 方法启动异步操作。When using the event-based asynchronous pattern for asynchronous operations, a Windows Forms form or control initiates an asynchronous operation by calling the BackgroundWorker.RunWorkerAsync method. 方法反过来异步引发 BackgroundWorker.DoWork 事件,并向其传递一个 DoWorkEventArgs 实例。The method in turn raises the BackgroundWorker.DoWork event asynchronously and passes it a DoWorkEventArgs instance. 如果异步操作返回一个值,则 BackgroundWorker.DoWork 事件处理程序通常会将其分配给 DoWorkEventArgs.Result 属性。If the asynchronous operation returns a value, the BackgroundWorker.DoWork event handler typically assigns it to the DoWorkEventArgs.Result property. 异步操作完成时,将引发 BackgroundWorker.RunWorkerCompleted 事件,并向其传递一个 RunWorkerCompletedEventArgs 实例,该实例包含有关操作状态的信息(是取消、出错还是成功完成)。When the asynchronous operation completes, the BackgroundWorker.RunWorkerCompleted event is raised and is passed a RunWorkerCompletedEventArgs instance that contains information about the status of the operation (whether it was cancelled, faulted, or completed successfully). 如果成功完成,则其 Result 属性包含异步操作返回的值,并且之前分配给 DoWorkEventArgs.Result 属性。If it completed successfully, its Result property contains the value returned by the asynchronous operation and previously assigned to the DoWorkEventArgs.Result property.

备注

应用于此类的 HostProtectionAttribute 特性具有以下 Resources 属性值: SharedStateThe HostProtectionAttribute attribute applied to this class has the following Resources property value: SharedState. HostProtectionAttribute 不影响桌面应用程序(通常通过双击图标、键入命令或在浏览器中输入 URL 来启动这些应用程序)。The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). 有关详细信息,请参阅 HostProtectionAttribute 类或SQL Server 编程和宿主保护属性For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

构造函数

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

初始化 RunWorkerCompletedEventArgs 类的新实例。Initializes a new instance of the RunWorkerCompletedEventArgs class.

属性

Cancelled

获取一个值,该值指示异步操作是否已被取消。Gets a value indicating whether an asynchronous operation has been canceled.

(继承自 AsyncCompletedEventArgs)
Error

获取一个值,该值指示异步操作期间发生的错误。Gets a value indicating which error occurred during an asynchronous operation.

(继承自 AsyncCompletedEventArgs)
Result

获取表示异步操作结果的值。Gets a value that represents the result of an asynchronous operation.

UserState

获取表示用户状态的值。Gets a value that represents the user state.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RaiseExceptionIfNecessary()

如果异步操作失败,则引发用户提供的异常。Raises a user-supplied exception if an asynchronous operation failed.

(继承自 AsyncCompletedEventArgs)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅