BackgroundWorker.RunWorkerCompleted イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > BackgroundWorker.RunWorkerCompleted イベントの意味・解説 

BackgroundWorker.RunWorkerCompleted イベント

メモ : このイベントは、.NET Framework version 2.0新しく追加されたものです。

バックグラウンド操作完了時、キャンセル時、またはバックグラウンド操作によって例外発生したときに発生します

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

Public Event RunWorkerCompleted As
 RunWorkerCompletedEventHandler
Dim instance As BackgroundWorker
Dim handler As RunWorkerCompletedEventHandler

AddHandler instance.RunWorkerCompleted, handler
public event RunWorkerCompletedEventHandler RunWorkerCompleted
public:
event RunWorkerCompletedEventHandler^ RunWorkerCompleted {
    void add (RunWorkerCompletedEventHandler^ value);
    void remove (RunWorkerCompletedEventHandler^ value);
}
/** @event */
public void add_RunWorkerCompleted (RunWorkerCompletedEventHandler
 value)

/** @event */
public void remove_RunWorkerCompleted (RunWorkerCompletedEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説

このイベントは、DoWork イベント ハンドラから戻るときに発生します

操作正常に完了しその結果DoWork イベント ハンドラ割り当てられると、RunWorkerCompletedEventArgs.Result プロパティからその結果アクセスできます

System.ComponentModel.RunWorkerCompletedEventArgs の Error プロパティは、操作によって例外スローされたことを示します

System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled プロパティは、バックグラウンド操作によってキャンセル要求処理されたかどうかを示しますDoWork イベント ハンドラ内のコードが CancellationPending フラグチェックし、System.ComponentModel.DoWorkEventArgs の Cancel フラグtrue設定することによってキャンセル要求検出した場合System.ComponentModel.RunWorkerCompletedEventArgsCancelled フラグtrue設定されます。

注意に関するメモ注意

DoWork イベント ハンドラ内のコードは、キャンセル要求が行われているときに処理を終了できることまた、ポーリング ループは、CancellationPendingtrue設定し損ねる場合があることに注意してください。この場合キャンセル要求が行われていても、RunWorkerCompleted イベント ハンドラSystem.ComponentModel.RunWorkerCompletedEventArgsCancelled フラグtrue設定されません。この状況競合状態呼ばれマルチスレッドプログラミングにおける一般的な懸念事項です。マルチスレッド デザインに関する問題詳細については、「マネージ スレッド処理の実施」を参照してください

RunWorkerCompleted イベント ハンドラは、RunWorkerCompletedEventArgs.Result プロパティアクセスする前に、常に AsyncCompletedEventArgs.Error プロパティおよび AsyncCompletedEventArgs.Cancelled プロパティチェックする必要があります例外発生した場合、または操作キャンセルされ場合RunWorkerCompletedEventArgs.Result プロパティアクセスすると例外発生します

使用例使用例

RunWorkerCompleted イベント使用して非同期操作結果処理するコード例次に示します。このコード例は、BackgroundWorker クラストピック取り上げているコード例一部分です。

' 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 Not (e.Error Is 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 'backgroundWorker1_RunWorkerCompleted
// 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.
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.get_Error() != null) {
    
        MessageBox.Show(e.get_Error().get_Message());
    }
    else {
    
        if (e.get_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.set_Text("Cancelled");
        }
        else {
        
            // Finally, handle the case where the operation 
            // succeeded.
            resultLabel.set_Text(e.get_Result().ToString());
        }
    }

    // Enable the UpDown control.
    this.numericUpDown1.set_Enabled(true);

    // Enable the Start button.
    startAsyncButton.set_Enabled(true);

    // Disable the Cancel button.
    cancelAsyncButton.set_Enabled(false);
} //backgroundWorker1_RunWorkerCompleted
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

BackgroundWorker.RunWorkerCompleted イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



BackgroundWorker.RunWorkerCompleted イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS