views:

317

answers:

4

Hello,

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 ?

+1  A: 

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.

Jan Bannister
+1  A: 

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.

Marc Gravell
The trick is that I don't want to start the tasks given their priorities, I want them to be performed given their prority. (that is to say a low-priority task should pause if a high priority one runs)
Toto
NB: I am using 3.5 framework
Toto
Then you'd have to look for "priority queue C#" - there are plenty, but I can't directly recommend any particular one. I *suspect* the TPL will offer this directly when released.
Marc Gravell
A: 

When you are using the build in ThreadPool all threads execute with the default priority. If you mess with this setting it will be ignored. This is a case where you should roll your own ThreadPool. A few years ago I extended the SmartThreadPool to meet my needs. This may satisfy yours as well.

Bob
A: 

I'd create a shared Queue of pending task objects, with each object specifying its priority. Then write a dispatcher thread that watches the Queue and launches a new thread for each task, up to some max thread limit, and specifying the thread priority as it creates it. Its only a small amount of work to do that, and you can have the dispatcher report activity and even dynamically adjust the number of running threads. That concept has worked very well for me, and can be wrapped in a windows service to boot if you make your queue a database table.

ebpower