共用方式為


RunWorkerCompletedEventArgs 類別

定義

提供 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

範例

以下程式碼範例說明了 的 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.
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.
    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.
    numericUpDown1.Enabled = True

    ' Enable the Start button.
    startAsyncButton.Enabled = True

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
End Sub

備註

當使用 基於事件的非同步模式 進行非同步操作時,Windows 表單或控制項會透過呼叫該 BackgroundWorker.RunWorkerAsync 方法來啟動非同步操作。 該方法反過來非 BackgroundWorker.DoWork 同步地將事件傳給它一個 DoWorkEventArgs 實例。 如果非同步操作回傳一個值, BackgroundWorker.DoWork 事件處理器通常會將該值指派到該 DoWorkEventArgs.Result 屬性上。 當非同步操作完成時, BackgroundWorker.RunWorkerCompleted 事件會被觸發並傳遞 RunWorkerCompletedEventArgs 一個包含操作狀態資訊的實例(是否已取消、錯誤或成功完成)。 若成功完成,其 Result 屬性包含非同步操作回傳且先前指派給該 DoWorkEventArgs.Result 屬性的值。

備註

HostProtectionAttribute套用於此類別的屬性具有以下Resources屬性值: SharedState。 這 HostProtectionAttribute 不會影響桌面應用程式(通常透過雙擊圖示、輸入指令或在瀏覽器輸入網址來啟動)。 欲了解更多資訊,請參閱 HostProtectionAttribute 類別或 SQL Server 程式設計與主機保護屬性

建構函式

名稱 Description
RunWorkerCompletedEventArgs(Object, Exception, Boolean)

初始化 RunWorkerCompletedEventArgs 類別的新執行個體。

屬性

名稱 Description
Cancelled

會得到一個值,表示非同步操作是否已被取消。

(繼承來源 AsyncCompletedEventArgs)
Error

會獲得一個值,表示非同步操作中發生了哪個錯誤。

(繼承來源 AsyncCompletedEventArgs)
Result

獲得一個代表非同步操作結果的值。

UserState

會得到一個代表使用者狀態的值。

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
RaiseExceptionIfNecessary()

若非同步操作失敗,則會觸發使用者提供的例外。

(繼承來源 AsyncCompletedEventArgs)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱