• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

BackgroundWorker Class

Can anyone show me an example of how to use the BackgroundWorker Class in the following scenario?

I'm calling a sub statement that looks something like this:

Sub DoSomething()
       For a as integer = 1 to 10
 
          DoSomeThingElseA()
          DoSomeThingElseB()

          ' Update 'a' on the screen
        Next
End Sub

When DoSomething is called I want to display 'a' on the screen.  I also want to show the status of DoSomeThingElseA and DoSomeThingElseB

I don't want the For Next Loop to advance until DoSomeThingElseA and DoSomeThingElseB
have completed there tasks.
0
Ray Turner
Asked:
Ray Turner
2 Solutions
 
Luis PérezSoftware Architect in .NetCommented:
Please, follow these steps:

1. Place a BackgroundWorker component inside your Form.
2. Set the WorkerReportsProgress property of the BackgroundWorker to True.
3. Double-click on the BackgroundWorker to open it's code screen and write this code into the BackgroundWorker1_DoWork event handler:

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    For a As Integer = 1 To 10
        DoSomeThingElseA()
        BackgroundWorker1.ReportProgress(0, "After DoSomeThingElseA")
        DoSomeThingElseB()
        BackgroundWorker1.ReportProgress(0, "After DoSomeThingElseB")
    Next
End Sub

Open in new window

Now write this code into the BackgroundWorker1_ProgressChanged event handler:

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Select Case DirectCast(e.UserState, String)
        Case "After DoSomeThingElseA"
            'Write your code here
        Case "After DoSomeThingElseB"
            'Write your code here
    End Select
End Sub

Open in new window

If you want to control when the BackgroundWorker finishes his work, then write this code into the BackgroundWorker1_RunWorkerCompleted event handler:

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    'The work has finished
End Sub

Open in new window

Finally, from somewhere in your Form, start the BackgroundWorker's job:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Me.BackgroundWorker1.RunWorkerAsync()
End Sub

Open in new window

Hope that helps.
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!  This is very helpful!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now