DoWorkEventHandler DoWorkEventHandler DoWorkEventHandler DoWorkEventHandler Delegate

Definizione

Rappresenta il metodo che gestirà l'evento DoWork.Represents the method that will handle the DoWork event. Questa classe non può essere ereditata.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)

Parametri

sender
Object Object Object Object

Origine dell'evento.The source of the event.

e
DoWorkEventArgs DoWorkEventArgs DoWorkEventArgs DoWorkEventArgs

Oggetto DoWorkEventArgs che contiene i dati dell'evento.A DoWorkEventArgs that contains the event data.

Ereditarietà
DoWorkEventHandlerDoWorkEventHandlerDoWorkEventHandlerDoWorkEventHandler

Esempi

Nell'esempio di codice riportato di seguito viene illustrato DoWorkEventHandler come utilizzare il delegato DoWork per gestire l'evento.The following code example demonstrates how to use the DoWorkEventHandler delegate to handle the DoWork event. Per un listato di codice completo , vedere Procedura: Eseguire un'operazione in background.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   

Commenti

Quando si crea un delegato DoWorkEventHandler, si identifica il metodo che gestirà l'evento.When you create a DoWorkEventHandler delegate, you identify the method that will handle the event. Per associare l'evento al gestore eventi in uso, aggiungere all'evento un'istanza del delegato.To associate the event with your event handler, add an instance of the delegate to the event. Il metodo del gestore eventi viene chiamato ogni volta che si verifica l'evento, a meno che non si rimuova il delegato.The event-handler method is called whenever the event occurs, unless you remove the delegate. Per ulteriori informazioni sui delegati del gestore eventi, vedere gestione e generazione di eventi.For more information about event-handler delegates, see Handling and Raising Events.

Metodi di estensione

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.Gets an object that represents the method represented by the specified delegate.

Si applica a

Vedi anche