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.
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 屬性值: SharedStateHostProtectionAttribute 不會影響桌面應用程式 (這些應用程式的啟動方式一般都是按兩下圖示、輸入命令或在瀏覽器輸入 URL)。 如需詳細資訊,請參閱 類別 HostProtectionAttributeSQL Server程式設計和主機保護屬性

建構函式

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

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

屬性

Cancelled

取得值,指出非同步作業是否已取消。

(繼承來源 AsyncCompletedEventArgs)
Error

取得值,指出非同步作業期間是否發生錯誤。

(繼承來源 AsyncCompletedEventArgs)
Result

取得值,表示非同步作業的結果。

UserState

取得表示使用者狀態的值。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RaiseExceptionIfNecessary()

如果非同步作業失敗,引發使用者提供的例外狀況。

(繼承來源 AsyncCompletedEventArgs)
ToString()

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

(繼承來源 Object)

適用於

另請參閱