By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Changing thread priority with ThreadPool threads?

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/de...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.

Jul 21 '05 #2

P: n/a
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.

"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:QV****************@cpmsftngxa07.phx.gbl...
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/de...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.

Jul 21 '05 #3

P: n/a
Hello Trev,

Thanks for your feedback.
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?

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.

Jul 21 '05 #4

P: n/a
Thanks again for your help.

Trev

"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:Nd**************@cpmsftngxa07.phx.gbl...
Hello Trev,

Thanks for your feedback.
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?

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.

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.