Solved

BackgroundWorker Class

Posted on 2013-12-19
3
324 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 69

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now