Solved

How to Get BackgroundWorker to Restart after Completed Event Fires

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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