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

DoWorkEventArgs,其中包含事件資料。

範例

下列程式碼範例示範如何使用 DoWorkEventHandler 委派來處理 DoWork 事件。 如需完整的程式代碼清單,請參閱 如何:在背景中執行作業

private 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 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非移除委派,否則每當事件發生時都會呼叫事件處理常式方法。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱