限制Task.Factory.Start中的任务数量

我已经看过一些关于一次限制任务数量的post( System.Threading.Tasks – 限制并发任务的数量是一个很好的)。

但是,我需要用秒来限制任务数量 – 每秒只有X个任务数量? 有一个简单的方法来实现这一目标吗?

我想创建一个ConcurrentDictionary,关键是当前的第二个,第二个是到目前为止的计数。 如果我们在当前秒为20,那么检查然后停止。 这似乎不是最理想的。

我宁愿每隔1秒/ 20点做一次任务。 有什么想法吗?

我想,这可以作为一个起点。 下面的示例创建了50个任务(每秒运行5个任务)。

这不会阻止创建任务的代码。 如果要在调度所有任务之前阻止调用者,则可以在Task.Delay((int)shouldWait).Wait()中使用Task.Delay((int)shouldWait).Wait()

 TaskFactory taskFactory = new TaskFactory(new TimeLimitedTaskScheduler(5)); for (int i = 0; i < 50; i++) { var x = taskFactory.StartNew(() => DateTime.Now.Second) .ContinueWith(t => Console.WriteLine(t.Result)); } Console.WriteLine("End of Loop"); 

 public class TimeLimitedTaskScheduler : TaskScheduler { int _TaskCount = 0; Stopwatch _Sw = null; int _MaxTasksPerSecond; public TimeLimitedTaskScheduler(int maxTasksPerSecond) { _MaxTasksPerSecond = maxTasksPerSecond; } protected override void QueueTask(Task task) { if (_TaskCount == 0) _Sw = Stopwatch.StartNew(); var shouldWait = (1000 / _MaxTasksPerSecond) * _TaskCount - _Sw.ElapsedMilliseconds; if (shouldWait < 0) { shouldWait = _TaskCount = 0; _Sw.Restart(); } Task.Delay((int)shouldWait) .ContinueWith(t => ThreadPool.QueueUserWorkItem((_) => base.TryExecuteTask(task))); _TaskCount++; } protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { return base.TryExecuteTask(task); } protected override IEnumerable GetScheduledTasks() { throw new NotImplementedException(); } }