BackgroundWorker / Thread question

P

Piotrekk

I have a question.

One day i have been working with threads. Thread had 'while' loop that
checked locked value telling the thread when application was closing
( in Dispose method i set locked object's value to 'true'). If i
didn't have the locked object the thread was present in taskmanager
even when application has been already gone.

My question is:

Having while loop in background worker i don't have to care about
closing conditions.
Even if i have while(true) and i close the application, the thread is
closed. Don't know why and when. Completed event is not raised - but
it normally it is during an exception for example.

Why is it so?

Thanks and regards
PK
 
C

Carl Daniel [VC++ MVP]

Piotrekk said:
I have a question.

One day i have been working with threads. Thread had 'while' loop that
checked locked value telling the thread when application was closing
( in Dispose method i set locked object's value to 'true'). If i
didn't have the locked object the thread was present in taskmanager
even when application has been already gone.

My question is:

Having while loop in background worker i don't have to care about
closing conditions.
Even if i have while(true) and i close the application, the thread is
closed. Don't know why and when. Completed event is not raised - but
it normally it is during an exception for example.

Why is it so?

Because background worker threads are automatically ended by the runtime
when the main thread exits. Look up the documentation on the
System.Threading.Thread.IsBackground property for more information on this
topic.

-cd
 
D

Doug Semler

Carl Daniel said:
Because background worker threads are automatically ended by the runtime
when the main thread exits. Look up the documentation on the
System.Threading.Thread.IsBackground property for more information on this
topic.

I thought it was that background threads are ended when all foreground
threads exit, not just the main thread? (All foreground threads exiting
indicates to the CLR to shutdown, right?)

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
P

Piotrekk

Thank you so much for the response.
One question remains... Is there anything that can't be done with
backgroundworker threads? Why to use regular thread instead of BW?
 
P

Piotrekk

Because background worker threads are automatically ended by the runtime
when the main thread exits. Look up the documentation on the
System.Threading.Thread.IsBackground property for more information on this
topic.


So when i want my thread to complete even if application will close i
should use foreground thread right?
The System.Threading.Thread.IsBackground is just what i was looking
for. Thank you.
 
P

Piotrekk

The System.Threading.Thread.IsBackground is just what i was looking
for. Thank you.
 
P

Peter Duniho

Piotrekk said:
Thank you so much for the response.
One question remains... Is there anything that can't be done with
backgroundworker threads? Why to use regular thread instead of BW?

BackgroundWorker uses the thread pool. The pool starts with a small
number of threads, and you get more as you start consuming them. The
thread pool is very useful for situations in which you repeatedly start
background tasks and don't want the overhead of creating new threads
over and over.

If you have a single task that you expect to take a long time, you might
as well just create a new thread yourself. It's not a good idea to set
IsBackground on a thread pool thread anyway, and a regular Thread
instance will be a foreground thread by default.

Pete
 
C

Carl Daniel [VC++ MVP]

Doug said:
"Carl Daniel [VC++ MVP]"
Because background worker threads are automatically ended by the
runtime when the main thread exits. Look up the documentation on the
System.Threading.Thread.IsBackground property for more information
on this topic.

I thought it was that background threads are ended when all foreground
threads exit, not just the main thread? (All foreground threads
exiting indicates to the CLR to shutdown, right?)

Yes, you're right about that - it's not just the main thread, but all
non-background thread that have to exit. (In the OPs case, I was assuming
that the main thread was the only non-background thread, but that's clearly
not true in the general case).

-cd
 

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

Top