Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# lower thread priority in thread pool

I have several low-imprtance tasks to be performed when some cpu time is available. I don't want this task to perform if other more import task are running. Ie if a normal/high priority task comes I want the low-importance task to pause until the importance task is done.

There is a pretty big number of low importance task to be performed (50 to 1000). So I don't want to create one thread per task. However I believe that the threadpool do not allow some priority specification, does it ?

How would you do solve this ?

like image 724
Toto Avatar asked Oct 16 '09 14:10

Toto


2 Answers

You can new up a Thread and use a Dispatcher to send it takes of various priorities.

The priorities are a bit UI-centric but that doesn't really matter.

like image 95
Jan Bannister Avatar answered Sep 29 '22 23:09

Jan Bannister


You shouldn't mess with the priority of the regular ThreadPool, since you aren't the only consumer. I suppose the logical approach would be to write your own - perhaps as simple as a producer/consumer queue, using your own Thread(s) as the consumer(s) - setting the thread priority yourself.

.NET 4.0 includes new libraries (the TPL etc) to make all this easier - until then you need additional code to create a custom thread pool or work queue.

like image 24
Marc Gravell Avatar answered Sep 30 '22 01:09

Marc Gravell