RunWorkerCompletedEventArgs 类

定义

为 MethodNameCompleted 事件提供数据。

public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
继承
RunWorkerCompletedEventArgs

示例

以下代码示例阐释了 RunWorkerCompletedEventArgs 的用法。 此示例是 类的较大示例的 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;
}

注解

基于事件的异步模式用于异步操作时,Windows 窗体窗体或控件通过调用 BackgroundWorker.RunWorkerAsync 方法启动异步操作。 方法反过来异步引发 事件, BackgroundWorker.DoWork 并向其传递实例 DoWorkEventArgs 。 如果异步操作返回值,则 BackgroundWorker.DoWork 事件处理程序通常会将其 DoWorkEventArgs.Result 分配给 属性。 异步操作完成后, BackgroundWorker.RunWorkerCompleted 将引发该事件,并向其传递一个 RunWorkerCompletedEventArgs 实例,该实例包含有关操作状态的信息 (该操作是否已取消、出错或成功完成) 。 如果成功完成,则其 Result 属性包含异步操作返回的值,并且之前分配给该 DoWorkEventArgs.Result 属性。

备注

HostProtectionAttribute应用于此类的属性具有以下Resources属性值:SharedStateHostProtectionAttribute 不影响桌面应用程序(通常通过双击图标、键入命令或在浏览器中输入 URL 来启动这些应用程序)。 有关详细信息,请参阅 HostProtectionAttribute 类或SQL Server编程和主机保护属性

构造函数

属性

Cancelled

获取一个值,该值指示异步操作是否已被取消。

(继承自 AsyncCompletedEventArgs)
Error

获取一个值,该值指示异步操作期间发生的错误。

(继承自 AsyncCompletedEventArgs)
Result

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

UserState

获取表示用户状态的值。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
RaiseExceptionIfNecessary()

如果异步操作失败,则引发用户提供的异常。

(继承自 AsyncCompletedEventArgs)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

另请参阅