Solved

How to Get BackgroundWorker to Restart after Completed Event Fires

Posted on 2008-09-30
3
649 Views
Last Modified: 2013-12-17
I have a simulation program that is running the simulations in the background using a BackgroundWorker. It works fine and the RunWorkerCompleted works as advertiesed. However, sometimes the user wants to run a new simulation. When the start button is pressed the parameters are checked and then the background worker is launched. On the second set of simulations the background worker immediately goes to the RunWorkerCompleted and does not execute the DoWork event. How can I reset the worker to allow it to do work once the RunWorkerCompleted event has fired?
Call after parameter checking:
 
 
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
        Me.Cursor = Cursors.WaitCursor
        BackgroundWorker1.RunWorkerAsync()
 
 
Background Worker code:
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        RunSim(worker, e)
 
    End Sub
 
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        SimProgressBar.Value = e.ProgressPercentage
        UpdateStatus(e.UserState)
    End Sub
 
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        txtTimeStopped.Text = Now.TimeOfDay.ToString
        Me.Cursor = Cursors.Default
        tabMainForm.SelectedIndex = 3
 
    End Sub

Open in new window

0
Comment
Question by:rkulp
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22610245
No special code is needed to make it "reset" and start from the beginning.  I just did a simple test and it worked as expected.

Most likely you have a logic bug in your RunSim() method...   =\
0
 
LVL 1

Author Comment

by:rkulp
ID: 22611213
Bob,

Thanks. You are correct. I found a file that had been opened but not closed. Closing it fixed the problem.
0
 
LVL 1

Author Closing Comment

by:rkulp
ID: 31501768
Thank you. As always, you hit the nail on the head.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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