BackgroundWorker.RunWorkerAsync Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Avvia l'esecuzione di un'operazione in background.
Overload
| Nome | Descrizione |
|---|---|
| RunWorkerAsync() |
Avvia l'esecuzione di un'operazione in background. |
| RunWorkerAsync(Object) |
Avvia l'esecuzione di un'operazione in background. |
RunWorkerAsync()
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- 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
IsBusy è true.
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.
void downloadButton_Click(object sender, EventArgs e)
{
// Start the download operation in the background.
backgroundWorker1.RunWorkerAsync();
// Disable the button for the duration of the download.
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 (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 genererà di nuovo un oggetto InvalidOperationException.
Vedi anche
- DoWork
- Come eseguire un'operazione in background
- Migliori pratiche per il threading gestito
- Come scaricare un file in modalità background
Si applica a
RunWorkerAsync(Object)
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- BackgroundWorker.cs
- Origine:
- 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 da utilizzare per l'operazione in background da eseguire nel DoWork gestore eventi.
Eccezioni
IsBusy è true.
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 a RunWorkerAsync.
Se l'operazione in background è già in esecuzione, la chiamata RunWorkerAsync genererà di nuovo un oggetto InvalidOperationException.
Vedi anche
- DoWork
- Come eseguire un'operazione in background
- Come scaricare un file in modalità background
- Migliori pratiche per il threading gestito