DoWorkEventHandler 委托

定义

表示将处理 DoWork 事件的方法。 无法继承此类。

public delegate void DoWorkEventHandler(System::Object ^ sender, DoWorkEventArgs ^ e);
public delegate void DoWorkEventHandler(object sender, DoWorkEventArgs e);
public delegate void DoWorkEventHandler(object? sender, DoWorkEventArgs e);
type DoWorkEventHandler = delegate of obj * DoWorkEventArgs -> unit
Public Delegate Sub DoWorkEventHandler(sender As Object, e As DoWorkEventArgs)

参数

sender
Object

事件源。

e
DoWorkEventArgs

包含事件数据的 A DoWorkEventArgs

示例

下面的代码示例演示如何使用 DoWorkEventHandler 委托来处理 DoWork 事件。 有关完整代码列表,请参阅 “如何:在后台运行操作”。

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Do not access the form's BackgroundWorker reference directly.
    // Instead, use the reference provided by the sender parameter.
    BackgroundWorker bw = sender as BackgroundWorker;

    // Extract the argument.
    int arg = (int)e.Argument;

    // Start the time-consuming operation.
    e.Result = TimeConsumingOperation(bw, arg);

    // If the operation was canceled by the user, 
    // set the DoWorkEventArgs.Cancel property to true.
    if (bw.CancellationPending)
    {
        e.Cancel = true;
    }
}
Private Sub backgroundWorker1_DoWork( _
sender As Object, e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork

   ' Do not access the form's BackgroundWorker reference directly.
   ' Instead, use the reference provided by the sender parameter.
   Dim bw As BackgroundWorker = CType( sender, BackgroundWorker )
   
   ' Extract the argument.
   Dim arg As Integer = Fix(e.Argument)
   
   ' Start the time-consuming operation.
   e.Result = TimeConsumingOperation(bw, arg)
   
   ' If the operation was canceled by the user, 
   ' set the DoWorkEventArgs.Cancel property to true.
   If bw.CancellationPending Then
      e.Cancel = True
   End If

End Sub

注解

创建 DoWorkEventHandler 委托时,可以标识将处理事件的方法。 若要将事件与事件处理程序相关联,请将委托的实例添加到事件。 除非删除委托,否则每当事件发生时调用事件处理程序方法。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

名称 说明
GetMethodInfo(Delegate)

获取一个对象,该对象表示由指定委托表示的方法。

适用于

另请参阅