Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Get BackgroundWorker to Restart after Completed Event Fires

Posted on 2008-09-30
3
Medium Priority
?
668 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
[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
  • 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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