Alw1n
asked on
BackgroundWorker thread completion and GUI thread interaction
Hi,
I have noticed some behaviour of the BackgroundWorker object in C# and want to confirm that what I see is true.
If have a GUI which has a loop to fire off several background workers that are stored in an array. I have for eg. 10 workers and 50 jobs so my gui sits in a loop to check when a worker is not busy and then re-uses that worker to do the next job. All fine.
What I noticed is that if I have no DoEvents in my loop then RunWorkerCompleted never fires and none of my threads ever get isBusy reset, with DoEvents it all works as expected.
1. Apparently using DoEvents is a bad idea although in my scenario it seems to be the best option?
2. I also have a short sleep in the loop to prevent CPU hogging - is this is necessary/good practice?
Pseudo code:
int jobCount = 30;
int jobs = 0;
while (jobs <= jobCount)
{
int i = bwFuncs.GetNextThread();
if (i < 0)
{
Application.DoEvents();
Thread.Sleep(50);
}
else
{
bwFuncs.threads|i|.RunWork erAsync();
jobs++;
}
}
I have noticed some behaviour of the BackgroundWorker object in C# and want to confirm that what I see is true.
If have a GUI which has a loop to fire off several background workers that are stored in an array. I have for eg. 10 workers and 50 jobs so my gui sits in a loop to check when a worker is not busy and then re-uses that worker to do the next job. All fine.
What I noticed is that if I have no DoEvents in my loop then RunWorkerCompleted never fires and none of my threads ever get isBusy reset, with DoEvents it all works as expected.
1. Apparently using DoEvents is a bad idea although in my scenario it seems to be the best option?
2. I also have a short sleep in the loop to prevent CPU hogging - is this is necessary/good practice?
Pseudo code:
int jobCount = 30;
int jobs = 0;
while (jobs <= jobCount)
{
int i = bwFuncs.GetNextThread();
if (i < 0)
{
Application.DoEvents();
Thread.Sleep(50);
}
else
{
bwFuncs.threads|i|.RunWork
jobs++;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the input everyone, I split the points in order of the replies received. I have ended up using the ThreadPool queue, it doesn't have all the control I was hoping for but seems to work quite well.
fyi I came across the library below which seems to have some cool things although I have not tried it.
http://smartthreadpool.codeplex.com/
fyi I came across the library below which seems to have some cool things although I have not tried it.
http://smartthreadpool.codeplex.com/
ASKER
In my scenario it is preferrable to fire my jobs from a loop and have a bit more control over how they are starting. I have never used the threadpool queue so will need to swat up a bit on that.