Solved

How to Get BackgroundWorker to Restart after Completed Event Fires

Posted on 2008-09-30
3
643 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now