Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how add tasks priority functionality

i use Task Library for my image compression service. I would to compress many files concurrency. But i want the service run only when user is idle(or no more impotarnt task in programm).

I know that threadPool does not support "change thread priority" feature, so task also does support this feature.

Can i develop that functionality on higher level of control?(TaskScheduler priority for example)

like image 983
Ivan Avatar asked Nov 01 '10 09:11

Ivan


People also ask

How do you assign priority when you have multiple tasks?

2 Organize tasks by priority You should give your most urgent and important tasks high priority and rank them toward the top of your list. You should give your least urgent tasks lower priority and place them near the bottom of the list.


2 Answers

As @zengr mentioned, you can use a priority queue pattern to solve this problem. There's actually a good sample in MSDN of implementing priority queues using ConcurrentQueue<T> instances per priority and then wrapping that with a custom IProducerConsumerCollection<T> implementation that pulls items from the higher priority queue before lower ones. This type of implementation enables your producer to determine how many priorities there should be, assign the priority when the item is added and lets your consumer work on the items with the highest priority first without having to ever understand the priority algorithm.

like image 64
Drew Marsh Avatar answered Sep 22 '22 06:09

Drew Marsh


You can create a custom TaskScheduler for the Task Parallel library and then schedule tasks on that by passing an instance of it to the TaskFactory constructor.

Here's one example of how to do that: Task Scheduler with priority

like image 22
Ian Mercer Avatar answered Sep 20 '22 06:09

Ian Mercer