更新:2007 年 11 月
命名空间:
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)
DoWork 事件处理程序返回时将引发此事件。
如果操作成功完成,并且其结果在 DoWork 事件处理程序中进行了分配,则可以通过 RunWorkerCompletedEventArgs..::.Result 属性访问该结果。
System.ComponentModel..::.RunWorkerCompletedEventArgs 的 Error 属性指示此操作已引发异常。
System.ComponentModel..::.RunWorkerCompletedEventArgs 的 Cancelled 属性指示后台操作是否已处理取消请求。如果通过检查 CancellationPending 标志并将 System.ComponentModel..::.DoWorkEventArgs 的 Cancel 标志设置为 true,DoWork 事件处理程序中的代码检测到取消请求,则 System.ComponentModel..::.RunWorkerCompletedEventArgs 的 Cancelled 标志也会设置为 true。
RunWorkerCompleted 事件处理程序应当总是先检查 AsyncCompletedEventArgs..::.Error 和 AsyncCompletedEventArgs..::.Cancelled 属性,然后再访问 RunWorkerCompletedEventArgs..::.Result 属性。如果引发了异常或者该操作已取消,则访问 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 (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 '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
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0
参考
其他资源