Changing thread priority with ThreadPool threads?

  • Thread starter Thread starter Trev Hunter
  • Start date Start date
T

Trev Hunter

Hi All,

I was just wondering if it is safe to change thread priorities with thread
pool threads (threads used by System.Threading.Timer and those used by the
ThreadPool class itself).

Say I have a background task that runs periodically (it uses
System.Threading.Timer to do this). As the execution speed of this task is
not a priority, I want to be able to set the priority of the current thread
to the lowest possible. If I use

----------
Thread.CurrentThread.Priority = ThreadPriority.Lowest
----------

to set the priority of the thread at the start of the background processing,
will this adversely affect the thread once it goes back into the thread
pool? Is there a chance that another task may come along and use this thread
and have its default priority set to whatever I last set it to, or does the
priority get reset when the thread is recycled? I don't know much about the
internal workings of the thread pool, so any help is welcome.


On another note, is there a better way of specifying what priority a
background task should be run with?

Thanks for your help,

Trev.
 
Hello Trev,

Thanks for your post.

Each thread in ThreadPool runs at the default priority and the code to
change the ThreadPriority has no effect. There are several scenarios in
which it is appropriate to create and manage your own threads instead of
using the ThreadPool. You should do so:

1. If you require a task to have a particular priority.
2. If you have a task that might run a long time (and therefore block other
tasks).
3. If you need to place threads into a single-threaded apartment (all
ThreadPool threads are in the multithreaded apartment).
4. If you need to have a stable identity associated with the thread. For
example, you might want to use a dedicated thread to abort that thread,
suspend it, or discover it by name.

Please refer to the following MSDN article for detailed information on
Thread Pooling:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconThreadPooling.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your help.

Taking your comments into account, is it recommended practice for a task
that runs on a perodic schedule to use System.Threading.Timer to fire
perodically, and then launch a separate thread to carry out the task?

Thanks again,

Trev.
 
Hello Trev,

Thanks for your feedback.
use System.Threading.Timer to fire perodically, and then launch a separate
thread to carry out the task?

It depends. As in this case that you need to change the thread priority,
you have to launch a separate thread to carry out the task. As listed in my
previous reply, generally speaking, there are four situations that you need
to create a thread. Otherwise, there is no need to do so.

I look forward to your feedback.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks again for your help.

Trev

Tian Min Huang said:
Hello Trev,

Thanks for your feedback.
to
use System.Threading.Timer to fire perodically, and then launch a separate
thread to carry out the task?

It depends. As in this case that you need to change the thread priority,
you have to launch a separate thread to carry out the task. As listed in my
previous reply, generally speaking, there are four situations that you need
to create a thread. Otherwise, there is no need to do so.

I look forward to your feedback.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top