DoWorkEventHandler Delegate

Definición

Representa el método que controlará el evento DoWork. Represents the method that will handle the DoWork event. Esta clase no puede heredarse. This class cannot be inherited.

public delegate void DoWorkEventHandler(object sender, DoWorkEventArgs e);
Parámetros
sender

Origen del evento. The source of the event.

e

Objeto DoWorkEventArgs que contiene los datos del evento. A DoWorkEventArgs that contains the event data.

Herencia
DoWorkEventHandler

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el DoWorkEventHandler delegado para controlar el DoWork eventos.The following code example demonstrates how to use the DoWorkEventHandler delegate to handle the DoWork event. Para obtener una lista de código completo, vea Cómo: ejecutar una operación en segundo plano.For a full code listing, see How to: Run an Operation in the Background.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Do not access the form's BackgroundWorker reference directly.
    // Instead, use the reference provided by the sender parameter.
    BackgroundWorker bw = sender as BackgroundWorker;

    // Extract the argument.
    int arg = (int)e.Argument;

    // Start the time-consuming operation.
    e.Result = TimeConsumingOperation(bw, arg);

    // If the operation was canceled by the user, 
    // set the DoWorkEventArgs.Cancel property to true.
    if (bw.CancellationPending)
    {
        e.Cancel = true;
    }
}
Private Sub backgroundWorker1_DoWork( _
sender As Object, e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork

   ' Do not access the form's BackgroundWorker reference directly.
   ' Instead, use the reference provided by the sender parameter.
   Dim bw As BackgroundWorker = CType( sender, BackgroundWorker )
   
   ' Extract the argument.
   Dim arg As Integer = Fix(e.Argument)
   
   ' Start the time-consuming operation.
   e.Result = TimeConsumingOperation(bw, arg)
   
   ' If the operation was canceled by the user, 
   ' set the DoWorkEventArgs.Cancel property to true.
   If bw.CancellationPending Then
      e.Cancel = True
   End If

End Sub   

Comentarios

Cuando se crea un delegado DoWorkEventHandler, se identifica el método que controlará el evento.When you create a DoWorkEventHandler delegate, you identify the method that will handle the event. Para asociar el evento al controlador, se debe agregar una instancia del delegado al evento.To associate the event with your event handler, add an instance of the delegate to the event. Se llama al método de controlador de eventos cada vez que se produce el evento, a menos que quite al delegado.The event-handler method is called whenever the event occurs, unless you remove the delegate. Para obtener más información sobre los delegados de controlador de eventos, consulte controlar y provocar eventos.For more information about event-handler delegates, see Handling and Raising Events.

Se aplica a

Consultar también