Solved

BackgroundWorker Class

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sign PDF document with digital PFX file from VB.NET 2010 1 29
Disable and re-enable a dynamic System.Timers.Timer 6 40
Showdialog 8 29
VS.net 2010 11 23
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

13 Experts available now in Live!

Get 1:1 Help Now