DoWorkEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表處理 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
事件的 來源。
DoWorkEventArgs A 包含事件資料。
範例
以下程式碼範例示範如何使用 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 代理人時,你就能確定處理事件的方法。 要將事件與你的事件處理器關聯,請將該代理的實例加入事件中。 事件處理方法在事件發生時會被呼叫,除非移除代理。 欲了解更多關於事件處理代表的資訊,請參閱 「處理與提升事件」。
擴充方法
| 名稱 | Description |
|---|---|
| GetMethodInfo(Delegate) |
取得一個代表指定代理所代表方法的物件。 |
適用於
另請參閱
- BackgroundWorker
- 如何在背景執行操作: