Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Jeśli masz operację, która potrwa długo i nie chcesz powodować opóźnień w interfejsie użytkownika, możesz użyć BackgroundWorker klasy , aby uruchomić operację w innym wątku.
Aby uzyskać pełną listę kodu używanego w tym przykładzie, zobacz Instrukcje: uruchamianie operacji w tle.
Uruchamianie operacji w tle
Gdy formularz jest aktywny w formularzu Windows Forms Projektant w programie Visual Studio, przeciągnij dwie Button kontrolki z przybornika do formularza, a następnie ustaw
Name
właściwości i Text przycisków zgodnie z poniższą tabelą.Przycisk Nazwisko Tekst button1
startBtn
Początek button2
cancelBtn
Anuluj Otwórz przybornik, kliknij kartę Składniki, a następnie przeciągnij BackgroundWorker składnik do formularza.
Składnik
backgroundWorker1
zostanie wyświetlony na pasku składników.W oknie Właściwości ustaw WorkerSupportsCancellation właściwość na
true
.W oknie Właściwości kliknij przycisk Zdarzenia, a następnie kliknij dwukrotnie zdarzenia DoWork iRunWorkerCompleted, aby utworzyć programy obsługi zdarzeń.
Wstaw czasochłonny kod do programu obsługi zdarzeń DoWork .
Wyodrębnij wszystkie parametry wymagane przez operację z Argument właściwości parametru DoWorkEventArgs .
Przypisz wynik obliczeń do Result właściwości DoWorkEventArgs.
Ta funkcja będzie dostępna dla programu obsługi zdarzeń RunWorkerCompleted .
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
Wstaw kod umożliwiający pobranie wyniku operacji w procedurze obsługi zdarzeń RunWorkerCompleted .
// This event handler demonstrates how to interpret // the outcome of the asynchronous operation implemented // in the DoWork event handler. private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { // The user canceled the operation. MessageBox.Show("Operation was canceled"); } else if (e.Error != null) { // There was an error during the operation. string msg = String.Format("An error occurred: {0}", e.Error.Message); MessageBox.Show(msg); } else { // The operation completed normally. string msg = String.Format("Result = {0}", e.Result); MessageBox.Show(msg); } }
' This event handler demonstrates how to interpret ' the outcome of the asynchronous operation implemented ' in the DoWork event handler. Private Sub backgroundWorker1_RunWorkerCompleted( _ sender As Object, e As RunWorkerCompletedEventArgs) _ Handles backgroundWorker1.RunWorkerCompleted If e.Cancelled Then ' The user canceled the operation. MessageBox.Show("Operation was canceled") ElseIf (e.Error IsNot Nothing) Then ' There was an error during the operation. Dim msg As String = String.Format("An error occurred: {0}", e.Error.Message) MessageBox.Show(msg) Else ' The operation completed normally. Dim msg As String = String.Format("Result = {0}", e.Result) MessageBox.Show(msg) End If End Sub
Zaimplementuj metodę
TimeConsumingOperation
.// This method models an operation that may take a long time // to run. It can be cancelled, it can raise an exception, // or it can exit normally and return a result. These outcomes // are chosen randomly. private int TimeConsumingOperation( BackgroundWorker bw, int sleepPeriod ) { int result = 0; Random rand = new Random(); while (!bw.CancellationPending) { bool exit = false; switch (rand.Next(3)) { // Raise an exception. case 0: { throw new Exception("An error condition occurred."); break; } // Sleep for the number of milliseconds // specified by the sleepPeriod parameter. case 1: { Thread.Sleep(sleepPeriod); break; } // Exit and return normally. case 2: { result = 23; exit = true; break; } default: { break; } } if( exit ) { break; } } return result; }
' This method models an operation that may take a long time ' to run. It can be cancelled, it can raise an exception, ' or it can exit normally and return a result. These outcomes ' are chosen randomly. Private Function TimeConsumingOperation( _ bw As BackgroundWorker, _ sleepPeriod As Integer) As Integer Dim result As Integer = 0 Dim rand As New Random() While Not bw.CancellationPending Dim [exit] As Boolean = False Select Case rand.Next(3) ' Raise an exception. Case 0 Throw New Exception("An error condition occurred.") Exit While ' Sleep for the number of milliseconds ' specified by the sleepPeriod parameter. Case 1 Thread.Sleep(sleepPeriod) Exit While ' Exit and return normally. Case 2 result = 23 [exit] = True Exit While Case Else Exit While End Select If [exit] Then Exit While End If End While Return result End Function
W Projektant formularzy systemu Windows kliknij
startButton
dwukrotnie, aby utworzyć procedurę obsługi zdarzeńClick.Wywołaj metodę RunWorkerAsync w procedurze Click obsługi zdarzeń dla elementu
startButton
.private void startBtn_Click(object sender, EventArgs e) { this.backgroundWorker1.RunWorkerAsync(2000); }
Private Sub startButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startBtn.Click Me.backgroundWorker1.RunWorkerAsync(2000) End Sub
W Projektant formularzy systemu Windows kliknij
cancelButton
dwukrotnie, aby utworzyć procedurę obsługi zdarzeńClick.Wywołaj metodę CancelAsync w procedurze Click obsługi zdarzeń dla elementu
cancelButton
.private void cancelBtn_Click(object sender, EventArgs e) { this.backgroundWorker1.CancelAsync(); }
Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cancelBtn.Click Me.backgroundWorker1.CancelAsync() End Sub
W górnej części pliku zaimportuj przestrzenie nazw System.ComponentModel i System.Threading.
using System; using System.ComponentModel; using System.Drawing; using System.Threading; using System.Windows.Forms;
Imports System.ComponentModel Imports System.Drawing Imports System.Threading Imports System.Windows.Forms
Naciśnij klawisz F6 , aby skompilować rozwiązanie, a następnie naciśnij klawisze Ctrl+F5 , aby uruchomić aplikację poza debugerem.
Uwaga
Jeśli naciśnij klawisz F5 , aby uruchomić aplikację w debugerze, wyjątek zgłoszony w
TimeConsumingOperation
metodzie zostanie przechwycony i wyświetlony przez debuger. Po uruchomieniu aplikacji poza debugerem BackgroundWorker program obsługuje wyjątek i buforuje ją we Error właściwości RunWorkerCompletedEventArgs.Kliknij przycisk Start, aby uruchomić operację asynchroniczną, a następnie kliknij przycisk Anuluj, aby zatrzymać uruchomioną operację asynchroniczną.
Wynik każdej operacji jest wyświetlany w obiekcie MessageBox.
Następne kroki
Zaimplementuj formularz, który zgłasza postęp jako operację asynchroniczną. Aby uzyskać więcej informacji, zobacz How to: Implement a Form That Uses a Background Operation (Instrukcje: implementowanie formularza korzystającego z operacji w tle).
Zaimplementuj klasę, która obsługuje asynchroniczny wzorzec dla składników. Aby uzyskać więcej informacji, zobacz Implementowanie wzorca asynchronicznego opartego na zdarzeniach.
Zobacz też
.NET Desktop feedback