开发者

how add tasks priority functionality

开发者 https://www.devze.com 2023-01-22 16:25 出处:网络
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 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 t开发者_运维技巧hread priority" feature, so task also does support this feature.

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号