DoWorkEventHandler Delegado

Definición

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

public delegate void DoWorkEventHandler(System::Object ^ sender, DoWorkEventArgs ^ e);
public delegate void DoWorkEventHandler(object sender, DoWorkEventArgs e);
type DoWorkEventHandler = delegate of obj * DoWorkEventArgs -> unit
Public Delegate Sub DoWorkEventHandler(sender As Object, e As DoWorkEventArgs)

Parámetros

sender
Object

Origen del evento.The source of the event.

e
DoWorkEventArgs

Valor 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 utilizar el delegado DoWorkEventHandler para controlar el evento de DoWork.The following code example demonstrates how to use the DoWorkEventHandler delegate to handle the DoWork event. Para obtener una lista de código completa, 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 control de eventos cada vez que se produce el evento, a menos que se quite el 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 controladores de eventos, vea controlar y provocar eventos.For more information about event-handler delegates, see Handling and Raising Events.

Métodos de extensión

GetMethodInfo(Delegate)

Obtiene un objeto que representa el método representado por el delegado especificado.Gets an object that represents the method represented by the specified delegate.

Se aplica a

Consulte también: