LimitedTaskScheduler:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Utils { public class LimitedTaskScheduler : TaskScheduler, IDisposable { [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); private BlockingCollection<Task> _tasks = new BlockingCollection<Task>(); private int _timeOut = Timeout.Infinite; public LimitedTaskScheduler() { for (int i = 0; i < 10; i++) { Thread thread = new Thread(new ThreadStart(() => { Task task; while (_tasks.TryTake(out task, _timeOut)) { TryExecuteTask(task); } })); thread.IsBackground = true; thread.Start(); } } protected override IEnumerable<Task> GetScheduledTasks() { return _tasks; } protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { return true; } protected override