using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace RoboSharp { #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member /// /// Object derived from TaskScheduler. Assisgns the task to some thread /// public class PriorityScheduler : TaskScheduler { /// TaskScheduler for AboveNormal Priority Tasks public static PriorityScheduler AboveNormal = new PriorityScheduler(ThreadPriority.AboveNormal); /// TaskScheduler for BelowNormal Priority Tasks public static PriorityScheduler BelowNormal = new PriorityScheduler(ThreadPriority.BelowNormal); /// TaskScheduler for the lowest Priority Tasks public static PriorityScheduler Lowest = new PriorityScheduler(ThreadPriority.Lowest); private BlockingCollection _tasks = new BlockingCollection(); private Thread[] _threads; private ThreadPriority _priority; private readonly int _maximumConcurrencyLevel = Math.Max(1, Environment.ProcessorCount); public PriorityScheduler(ThreadPriority priority) { _priority = priority; } public override int MaximumConcurrencyLevel { get { return _maximumConcurrencyLevel; } } protected override IEnumerable GetScheduledTasks() { return _tasks; } protected override void QueueTask(Task task) { _tasks.Add(task); bool _executing = false; if (_threads == null) { _threads = new Thread[_maximumConcurrencyLevel]; for (int i = 0; i < _threads.Length; i++) { int local = i; _threads[i] = new Thread(() => { foreach (Task t in _tasks.GetConsumingEnumerable()) _executing = base.TryExecuteTask(t); }); _threads[i].Name = string.Format("PriorityScheduler: ", i); _threads[i].Priority = _priority; _threads[i].IsBackground = true; _threads[i].Start(); } } } protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { return false; // we might not want to execute task that should schedule as high or low priority inline } } #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member }