?
Solved

Calling a Backgroundworker from a DoWork event of another BackgroundWorker

Posted on 2010-01-13
3
Medium Priority
?
483 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:DaemonFish
[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
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26304184
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
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1500 total points
ID: 26309169
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
 
LVL 3

Author Closing Comment

by:DaemonFish
ID: 31676612
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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