Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

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?
0
DaemonFish
Asked:
DaemonFish
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now