BackgroundWorker


The BackgroundWorker component simplifies multithreading. When a program invokes its RunWorker?Async method, the component starts running on a new thread. It raises its DoWork method on the new thread, and the corresponding event handler should perform the necessary work. While it runs, the worker can call the component’s ReportProgress method to raise a ProgressChanged event on the main thread to let the program know how it is progressing.

When the worker thread finishes, the component receives a RunWorkerCompleted event on the main thread.

The following code demonstrates the BackgroundWorker component. When the user clicks the Start button, the program disables its Start button, enables its Cancel button, and sets its ProgressBar’s Value property to 0. It then prepares the bgrLongProcess BackgroundWorker component and calls its RunWorkerAsync method. If the user clicks the Cancel button while the worker is running, the program calls the BackgroundWorker’s CancelAsync method to tell it to stop running. As the BackgroundWorker runs, it raises ProgressChanged events. When the main thread receives one of these events, it sets its ProgressBar’s Value property to display the worker’s progress. When the BackgroundWorker finishes, the RunWorkerCompleted event handler enables the Start button and disables the Cancel button.

All the code described so far runs on the main program thread. The worker’s DoWork event handler runs on the worker thread. In this example, the code loops for several seconds. Each time through the loop, the program waits for one second. It then checks the worker component’s CancellationPending property to see if the user clicked the Cancel button. If CancellationPending is True, then the event handler exits and the worker is done.

If CancellationPending is False, then the event handler calls the worker’s ReportProgress method to tell the main thread how far it has gotten.

When the For loop ends, the event handler exits and the worker stops executing.

  ' Start the long process. Private Sub btnStartProcess_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnStartProcess.Click     ' Get ready.     btnStartProcess.Enabled = False     btnCancel.Enabled = True     prgLongProcess.Value = 0     ' Start the worker.     bgrLongProcess.WorkerReportsProgress = True     bgrLongProcess.WorkerSupportsCancellation = True     bgrLongProcess.RunWorkerAsync() End Sub ' Cancel the long process. Private Sub btnCancel_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnCancel.Click     bgrLongProcess.CancelAsync() End Sub ' Display the progress. Private Sub bgrLongProcess_ProgressChanged(ByVal sender As Object, _  ByVal e As System.ComponentModel.ProgressChangedEventArgs) _  Handles bgrLongProcess.ProgressChanged     prgLongProcess.Value = e.ProgressPercentage End Sub ' The worker is done. Private Sub bgrLongProcess_RunWorkerCompleted(ByVal sender As Object, _  ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _  Handles bgrLongProcess.RunWorkerCompleted     btnStartProcess.Enabled = True     btnCancel.Enabled = False End Sub ' Do the work. Private Sub bgrLongProcess_DoWork(ByVal sender As Object, _  ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgrLongProcess.DoWork     ' This example just wastes some time.     Const NUM_SECONDS As Integer = 5     For i As Integer = 1 To NUM_SECONDS         ' Pause 1 second.         Dim wait_until As Date = Now.AddSeconds(1)         Do While Now < wait_until         Loop         ' If the user has canceled, stop.         If bgrLongProcess.CancellationPending Then Exit Sub         ' Report our progress.         bgrLongProcess.ReportProgress(CInt(100 * i / NUM_SECONDS))     Next i End Sub 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net