Solved

BackgroundWorker Class

Posted on 2013-12-19
3
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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