BackgroundWorker.RunWorkerAsync Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Elindítja a háttérművelet végrehajtását.
Túlterhelések
| Name | Description |
|---|---|
| RunWorkerAsync() |
Elindítja a háttérművelet végrehajtását. |
| RunWorkerAsync(Object) |
Elindítja a háttérművelet végrehajtását. |
RunWorkerAsync()
Elindítja a háttérművelet végrehajtását.
public:
void RunWorkerAsync();
public void RunWorkerAsync();
member this.RunWorkerAsync : unit -> unit
Public Sub RunWorkerAsync ()
Kivételek
IsBusy az true.
Példák
Az alábbi példakód bemutatja, hogy a RunWorkerAsync metódus hogyan indít el aszinkron műveletet. Ez egy nagyobb példa része, amelyet a How to: Download a File in the Background (Fájl letöltése a háttérben) című cikkben ismertetünk.
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
Megjegyzések
A RunWorkerAsync metódus kérést küld a művelet aszinkron futtatásának elindítására. A kérés kiszolgálása után az DoWork esemény előjön, ami elindítja a háttérművelet végrehajtását.
Ha a háttérművelet már fut, az ismételt hívás RunWorkerAsync egy InvalidOperationException.
Lásd még
- DoWork
- Útmutató: Művelet futtatása a háttérben
- felügyelt szálkezelés ajánlott eljárásai
- Útmutató: Fájl letöltése a háttérben
A következőre érvényes:
RunWorkerAsync(Object)
Elindítja a háttérművelet végrehajtását.
public:
void RunWorkerAsync(System::Object ^ argument);
public void RunWorkerAsync(object argument);
member this.RunWorkerAsync : obj -> unit
Public Sub RunWorkerAsync (argument As Object)
Paraméterek
Kivételek
IsBusy az true.
Példák
Az alábbi példakód bemutatja, hogy a RunWorkerAsync metódus hogyan indít el aszinkron műveletet. Ez a példakód egy nagyobb, az BackgroundWorker osztályhoz tartozó példa része.
// Start the asynchronous operation.
backgroundWorker1->RunWorkerAsync( numberToCompute );
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute);
' Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute)
Megjegyzések
A RunWorkerAsync metódus kérést küld a művelet aszinkron futtatásának elindítására. A kérés kiszolgálása után az DoWork esemény előjön, ami elindítja a háttérművelet végrehajtását.
Ha a művelethez paraméterre van szükség, megadhatja paraméterként.argumentRunWorkerAsync
Ha a háttérművelet már fut, az ismételt hívás RunWorkerAsync egy InvalidOperationException.
Lásd még
- DoWork
- Útmutató: Művelet futtatása a háttérben
- Útmutató: Fájl letöltése a háttérben
- felügyelt szálkezelés ajánlott eljárásai