Solved

BackgroundWorker Class

Posted on 2013-12-19
3
329 Views
Last Modified: 2013-12-20
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
Comment
Question by:Ray Turner
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 250 total points
ID: 39731258
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
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 39731462
0
 

Author Closing Comment

by:Ray Turner
ID: 39731545
Thanks!  This is very helpful!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question