I think, this can be a starting point. Below sample creates 50 Tasks (running 5 tasks/sec).
This doesn't block the code that creates the tasks. If you want to block the caller until all task have been scheduled, then you can use Task.Delay((int)shouldWait).Wait()
in 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();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…