RunWorkerCompletedEventArgs Classe
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Fornece dados para o evento MethodNameCompleted .
public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
- Herança
Exemplos
O seguinte exemplo de código ilustra o uso de RunWorkerCompletedEventArgs. Este exemplo faz parte de uma amostra maior para a BackgroundWorker turma.
// 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
Observações
Ao usar o padrão assíncrono baseado em eventos para operações assíncronas, uma forma Windows Forms ou controlo inicia uma operação assíncrona ao chamar o método BackgroundWorker.RunWorkerAsync. O método, por sua vez, eleva o BackgroundWorker.DoWork evento de forma assíncrona e passa-lhe uma DoWorkEventArgs instância. Se a operação assíncrona devolver um valor, o BackgroundWorker.DoWork gestor de eventos normalmente atribui-o à DoWorkEventArgs.Result propriedade. Quando a operação assíncrona termina, o BackgroundWorker.RunWorkerCompleted evento é levantado e recebe uma RunWorkerCompletedEventArgs instância que contém informações sobre o estado da operação (se foi cancelada, falhada ou concluída com sucesso). Se for concluída com sucesso, a sua Result propriedade contém o valor devolvido pela operação assíncrona e previamente atribuído à DoWorkEventArgs.Result propriedade.
Observação
O HostProtectionAttribute atributo aplicado a esta classe tem o seguinte Resources valor de propriedade: SharedState. Isto HostProtectionAttribute não afeta aplicações de ambiente de trabalho (que normalmente são iniciadas com duplo clique num ícone, digitação de um comando ou introdução de um URL no navegador). Para mais informações, consulte a classe HostProtectionAttribute ou SQL Server Atributos de Programação e Proteção de Host.
Construtores
| Name | Description |
|---|---|
| RunWorkerCompletedEventArgs(Object, Exception, Boolean) |
Inicializa uma nova instância da RunWorkerCompletedEventArgs classe. |
Propriedades
| Name | Description |
|---|---|
| Cancelled |
Recebe um valor que indica se uma operação assíncrona foi cancelada. (Herdado de AsyncCompletedEventArgs) |
| Error |
Recebe um valor que indica qual erro ocorreu durante uma operação assíncrona. (Herdado de AsyncCompletedEventArgs) |
| Result |
Obtém um valor que representa o resultado de uma operação assíncrona. |
| UserState |
Obtém um valor que representa o estado do utilizador. |
Métodos
| Name | Description |
|---|---|
| Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual. (Herdado de Object) |
| GetHashCode() |
Serve como função de hash predefinida. (Herdado de Object) |
| GetType() |
Obtém o Type da instância atual. (Herdado de Object) |
| MemberwiseClone() |
Cria uma cópia superficial do atual Object. (Herdado de Object) |
| RaiseExceptionIfNecessary() |
Levanta uma exceção fornecida pelo utilizador se uma operação assíncrona falhou. (Herdado de AsyncCompletedEventArgs) |
| ToString() |
Devolve uma cadeia que representa o objeto atual. (Herdado de Object) |