background worker thread

  • Thread starter Thread starter Analizer1
  • Start date Start date
A

Analizer1

From what im reading about backgroundworker thread
it seems it can only have 1 thread at a time

is this correct
tks
 
Analizer1 said:
From what im reading about backgroundworker thread
it seems it can only have 1 thread at a time

is this correct

No.

What's more to say? You can have as many as the system reasonably allows. If
there's something else you're confused about, do ask.
 
Jeroen said:
No.

What's more to say? You can have as many as the system reasonably
allows. If there's something else you're confused about, do ask.
Of course, spacing is vital here. Do you mean a "background worker thread",
or "the thread used for a BackgroundWorker instance"?

Every BackgroundWorker instance is backed by only one thread, but you can
have multiple BackgroundWorkers without any problem.
 
public ThreadWorker: BackgroundWorker

im using the above BackgroundWorker

small sample from the threadManager class

for (int x=0:x<this.aWorkers.GetLength(0);x++)
{
if (aWorkers[x].IsBusy=false)
{
int iJobNum = SombojectGetJobNum()
if (iJobNum>0)
{
aWorkers[x].RunWorkerAsync(iJobNum)
}
}
//as it assigns more jobs i get -> This BackgroundWorker is
currently busy and cannot run multiple tasks concurrently.
//it assign's a job , next one is complete or not busy and
assigns a new job
// It also seems to be assigning jobs to a busy thread

}
 
[...]
//as it assigns more jobs i get -> This BackgroundWorker is
currently busy and cannot run multiple tasks concurrently.

As the error explains, a given BackgroundWorker can only be running a
single background task at a time. As Jeroen points out, you can create as
many BackgroundWorker instances as you like.

There will always be a limit as to the maximum number of threads you can
have running at any given time. So even creating a new BackgroundWorker
for each task you want to start, eventually you'll exhaust the thread pool
and new BackgroundWorker instances will have to wait for
previously-started ones to complete before they can themselves start. But
that's actually a good thing.

Also, if your background task is CPU-bound, you don't really want to start
a whole bunch of them all at once anyway. For CPU-bound stuff, it's
counter-productive to have more of them running than you have CPU cores,
at least from a through-put point of view.

Pete
 
i have only created 5 backbgroundworker threads
I ran 100 jobs and 3 crashed with the previous error i posted..so im just
trying to track down the cause.

The threads actually call a couple of stored procs sql server 2005 sp2 and
update rows with new status Codes

tks



Peter Duniho said:
[...]
//as it assigns more jobs i get -> This BackgroundWorker is
currently busy and cannot run multiple tasks concurrently.

As the error explains, a given BackgroundWorker can only be running a
single background task at a time. As Jeroen points out, you can create as
many BackgroundWorker instances as you like.

There will always be a limit as to the maximum number of threads you can
have running at any given time. So even creating a new BackgroundWorker
for each task you want to start, eventually you'll exhaust the thread pool
and new BackgroundWorker instances will have to wait for
previously-started ones to complete before they can themselves start. But
that's actually a good thing.

Also, if your background task is CPU-bound, you don't really want to start
a whole bunch of them all at once anyway. For CPU-bound stuff, it's
counter-productive to have more of them running than you have CPU cores,
at least from a through-put point of view.

Pete
 
I got it working Right....Thank you for your help
in the ThreadCompleted Event
i had a Call To StartNew Jobs...instead of Enableing The Timer Event

thanks again

Peter Duniho said:
[...]
//as it assigns more jobs i get -> This BackgroundWorker is
currently busy and cannot run multiple tasks concurrently.

As the error explains, a given BackgroundWorker can only be running a
single background task at a time. As Jeroen points out, you can create as
many BackgroundWorker instances as you like.

There will always be a limit as to the maximum number of threads you can
have running at any given time. So even creating a new BackgroundWorker
for each task you want to start, eventually you'll exhaust the thread pool
and new BackgroundWorker instances will have to wait for
previously-started ones to complete before they can themselves start. But
that's actually a good thing.

Also, if your background task is CPU-bound, you don't really want to start
a whole bunch of them all at once anyway. For CPU-bound stuff, it's
counter-productive to have more of them running than you have CPU cores,
at least from a through-put point of view.

Pete
 
i have only created 5 backbgroundworker threads

Did you create threads? Or instances of the BackgroundWorker class?

The two are not synonymous.
I ran 100 jobs and 3 crashed with the previous error i posted..so im just
trying to track down the cause.

The issue is as I described. If you get that error, it's because you're
trying to start a new task on a BackgroundWorker that hasn't finished its
previous task.

The way to fix it is to not do that.

Pete
 
thanks for your help. peter

Peter Duniho said:
Did you create threads? Or instances of the BackgroundWorker class?

The two are not synonymous.


The issue is as I described. If you get that error, it's because you're
trying to start a new task on a BackgroundWorker that hasn't finished its
previous task.

The way to fix it is to not do that.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top