我已经看过一些关于一次限制任务数量的帖子(System.Threading.Tasks - Limit the number of concurrent Tasks是一个很好的帖子)。
但是,我需要限制任务数量 - 每秒只有X个任务数量?有没有一种简单的方法可以实现这一目标?
我考虑创建一个ConcurrentDictionary,键是当前的第二个,第二个是到目前为止的计数。检查我们是否在当前秒为20,然后停止。这似乎不是最理想的。
我宁愿每隔1秒/ 20点做一次任务。有什么想法吗?
答案 0 :(得分:3)
我认为,这可以作为一个起点。下面的示例创建了50个任务(每秒运行5个任务)。
这不会阻止创建任务的代码。如果您想要在调度所有任务之前阻止来电者,那么您可以在Task.Delay((int)shouldWait).Wait()
中使用QueueTask
TaskFactory taskFactory = new TaskFactory(new TimeLimitedTaskScheduler(5));
for (int i = 0; i < 50; i++)
{
var x = taskFactory.StartNew<int>(() => 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<Task> GetScheduledTasks()
{
throw new NotImplementedException();
}
}