Calling a Backgroundworker from a DoWork event of another BackgroundWorker

I have a small problem when calling a Backgroundworker from a DoWork event of another BackgroundWorker.

The problem is that when the DoWork method of the second BackgroundWorker is finished, the RunWorkerCompleted event is not invoked. When I start the same BackgroundWorker from another place in my program (not from inside a BackgroundWorker), then RunWorkerCompleted is invoked as expected.

Do anyone know any way around this problem, or at least the reason why the BackgroundWorker behaves like this?
LVL 3
DaemonFishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
It probably needs to be started from the main UI thread so it knows which thread to Invoke() to.  You could make the first BackgroundWorker() call ReportProgress() with some kind of flag that tells the main UI to start the secondary BackgroundWorker() for you.

If that doesn't work, can you show us some example code so we can attempt to reproduce the problem?
0
Miguel OzSoftware EngineerCommented:
You need to create both BackgroundWorker in the main UI.
You can not call a BackgroundWorker object from a thread (or from DoWork in this case)

If you need to start 2nd BackgroundWorker somewhere in the middle of your DoWork 1st BackgroundWorker
I will suggest you use ManualResetEvent object as follows:
//m_wait  is ManualResetEvent object created in your main UI and pass as a parameter to both BackgroundWorker objs
DoWork 1st BackgroundWorker
{
//Some code
m_wait.Set();
//some code
}
DoWork 2st BackgroundWorker
{
  m_wait.WaitOne();
  //Your code as it is now
}

Basically once you started both BackgroundWorker at the same time in your main UI , the 2nd BackgroundWorker object will wait until the event is fired then it will continue processing as per current code.
check:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaemonFishAuthor Commented:
This did not solve my problem, but it did at least give me enough insight into how BackgroundWorkers work so that I could modify the basic structure of the program to make it work as it should.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.