BackgroundWorker.RunWorkerAsync Metodo

Definizione

Avvia l'esecuzione di un'operazione in background.

Overload

RunWorkerAsync()

Avvia l'esecuzione di un'operazione in background.

RunWorkerAsync(Object)

Avvia l'esecuzione di un'operazione in background.

RunWorkerAsync()

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs

Avvia l'esecuzione di un'operazione in background.

public:
 void RunWorkerAsync();
public void RunWorkerAsync ();
member this.RunWorkerAsync : unit -> unit
Public Sub RunWorkerAsync ()

Eccezioni

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del RunWorkerAsync metodo per avviare un'operazione asincrona. Fa parte di un esempio più ampio descritto in Procedura: Scaricare un file in background.

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
Private Sub downloadButton_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles downloadButton.Click

    ' Start the download operation in the background.
    Me.backgroundWorker1.RunWorkerAsync()

    ' Disable the button for the duration of the download.
    Me.downloadButton.Enabled = False

    ' Once you have started the background thread you 
    ' can exit the handler and the application will 
    ' wait until the RunWorkerCompleted event is raised.

    ' If you want to do something else in the main thread,
    ' such as update a progress bar, you can do so in a loop 
    ' while checking IsBusy to see if the background task is
    ' still running.
    While Me.backgroundWorker1.IsBusy
        progressBar1.Increment(1)
        ' Keep UI messages moving, so the form remains 
        ' responsive during the asynchronous operation.
        Application.DoEvents()
    End While
End Sub

Commenti

Il RunWorkerAsync metodo invia una richiesta per avviare l'operazione in modo asincrono. Quando viene eseguita la richiesta, viene generato l'evento DoWork , che a sua volta avvia l'esecuzione dell'operazione in background.

Se l'operazione in background è già in esecuzione, la chiamata RunWorkerAsync genera nuovamente un oggetto InvalidOperationException.

Vedi anche

Si applica a

RunWorkerAsync(Object)

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs

Avvia l'esecuzione di un'operazione in background.

public:
 void RunWorkerAsync(System::Object ^ argument);
public void RunWorkerAsync (object argument);
public void RunWorkerAsync (object? argument);
member this.RunWorkerAsync : obj -> unit
Public Sub RunWorkerAsync (argument As Object)

Parametri

argument
Object

Parametro che deve essere utilizzato dall'operazione in background da eseguire nell'evento DoWork.

Eccezioni

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del RunWorkerAsync metodo per avviare un'operazione asincrona. Questo esempio di codice fa parte di un esempio più ampio fornito per la BackgroundWorker classe .

// Start the asynchronous operation.
backgroundWorker1->RunWorkerAsync( numberToCompute );
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute);

' Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute)

Commenti

Il RunWorkerAsync metodo invia una richiesta per avviare l'operazione in modo asincrono. Quando viene eseguita la richiesta, viene generato l'evento DoWork , che a sua volta avvia l'esecuzione dell'operazione in background.

Se l'operazione richiede un parametro, è possibile specificarlo come argument parametro per RunWorkerAsync.

Se l'operazione in background è già in esecuzione, la chiamata RunWorkerAsync genera nuovamente un oggetto InvalidOperationException.

Vedi anche

Si applica a