Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Get BackgroundWorker to Restart after Completed Event Fires

Posted on 2008-09-30
3
Medium Priority
?
670 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 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