閱讀英文

共用方式為


DoWorkEventHandler 代理人

定義

表示處理 DoWork 事件的方法。 此類別無法獲得繼承。

C#
public delegate void DoWorkEventHandler(object sender, DoWorkEventArgs e);
C#
public delegate void DoWorkEventHandler(object? sender, DoWorkEventArgs e);

參數

sender
Object

事件的來源。

e
DoWorkEventArgs

DoWorkEventArgs,其中包含事件資料。

範例

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

C#
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;
    }
}

備註

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

擴充方法

GetMethodInfo(Delegate)

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

適用於

產品 版本
.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

另請參閱