RunWorkerCompletedEventArgs 類別
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 MethodNameCompleted
事件的數據。
public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
- 繼承
下列程式碼範例會說明使用 RunWorkerCompletedEventArgs。 這個範例是 類別較大範例的 BackgroundWorker 一部分。
// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted( Object^ /*sender*/, RunWorkerCompletedEventArgs^ e )
{
// First, handle the case where an exception was thrown.
if ( e->Error != nullptr )
{
MessageBox::Show( e->Error->Message );
}
else
if ( e->Cancelled )
{
// Next, handle the case where the user cancelled
// 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 = "Cancelled";
}
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;
}
// 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;
}
' This event handler deals with the results of the
' background operation.
Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
' First, handle the case where an exception was thrown.
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then
' 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()
End If
' Enable the UpDown control.
Me.numericUpDown1.Enabled = True
' Enable the Start button.
startAsyncButton.Enabled = True
' Disable the Cancel button.
cancelAsyncButton.Enabled = False
End Sub
針對異步操作使用事件架構異步模式時,Windows Forms 窗體或控件會呼叫 BackgroundWorker.RunWorkerAsync 方法來起始異步操作。 方法接著會以異步方式引發 事件, BackgroundWorker.DoWork 並傳遞 DoWorkEventArgs 實例。 如果異步操作傳回值, BackgroundWorker.DoWork 事件處理程式通常會將它指派給 DoWorkEventArgs.Result 屬性。 異步操作完成時, BackgroundWorker.RunWorkerCompleted 會引發 事件,並傳遞 RunWorkerCompletedEventArgs 實例,其中包含作業狀態的相關信息, (作業是否已取消、錯誤或成功完成) 。 如果成功完成,其 Result 屬性會包含異步操作所傳回的值,而且先前指派給 DoWorkEventArgs.Result 屬性。
備註
HostProtectionAttribute套用至這個類別的屬性具有下列Resources屬性值:SharedState。 HostProtectionAttribute 不會影響桌面應用程式 (這些應用程式的啟動方式一般都是按兩下圖示、輸入命令或在瀏覽器輸入 URL)。 如需詳細資訊,請參閱 類別HostProtectionAttribute或 SQL Server 程式設計和主機保護屬性。
Cancelled |
取得值,指出非同步作業是否已取消。 (繼承來源 AsyncCompletedEventArgs) |
Error |
取得值,指出非同步作業期間是否發生錯誤。 (繼承來源 AsyncCompletedEventArgs) |
Result |
取得值,表示非同步作業的結果。 |
User |
取得表示使用者狀態的值。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Raise |
如果非同步作業失敗,引發使用者提供的例外狀況。 (繼承來源 AsyncCompletedEventArgs) |
To |
傳回代表目前物件的字串。 (繼承來源 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 |