限制同时执行任务的数量

考虑这个庞大的任务池:

var tasks = new Task[4] { Task.Factory.StartNew(() => DoSomething()), Task.Factory.StartNew(() => DoSomething()), Task.Factory.StartNew(() => DoSomething()), Task.Factory.StartNew(() => DoSomething()), Task.Factory.StartNew(() => DoSomething()) }; Task.WaitAll(tasks); 

如果我只想同时说3个任务怎么办? 我将如何在代码中实现它?

比MSDN版本更复杂的例子是使用Parallel.Invoke设置最大并行度:

 Parallel.Invoke( new ParallelOptions() { MaxDegreeOfParallelism = 3 }, () => DoSomething(), () => DoSomething(), () => DoSomething(), () => DoSomething(), () => DoSomething()); 

然而,Parallel.Invoke()将阻塞,直到完成所有并行操作(这意味着除了parallel.invoke之外的任何代码都不会运行,直到它们全部完成)。 如果这对您不起作用,那么最终需要创建自己的任务调度程序,如Daniel所链接的MSDN文章所示。

我在MSDN上找到了这个例子 。 我相信它实现了你想要实现的目标。

所以你想指定同时任务的数量。 请注意,这是一个糟糕的设计理念 – 至少在大多数情况下,您应该让系统决定同时执行多少任务。 当您使用Task.Factory.StartNew方法以这种方式创建任务而没有其他参数时,它们应尽快执行(尽快),因此通常不应明确指定它们同时执行的编号。

在这种情况下ASAP是什么意思? 任务管理器将决定是立即启动所有这些,还是将其中的一些启动,或等待其他任务完成等。

您可以使用某种手动同步来实现您的目标。 我的意思是像信号量。 http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

如果您不需要做其他工作并且只想等待任务完成,我更喜欢Parallel.Invoke如Gary S.所述。

我的博客文章展示了如何使用“任务”和“操作”执行此操作,并提供了一个示例项目,您可以下载并运行以查看两者的实际操作。

原始海报没有说明他们是否更喜欢使用动作或任务,有时候从一个切换到另一个并不容易,所以我在这里提出了两个解决方案。

有了动作

如果使用Actions,则可以使用内置的.Net Parallel.Invoke函数。 在这里,我们将其限制为最多并行运行3个线程。

 var listOfActions = new List(); for (int i = 0; i < 10; i++) { // Note that we create the Action here, but do not start it. listOfActions.Add(() => DoSomething()); } var options = new ParallelOptions {MaxDegreeOfParallelism = 3}; Parallel.Invoke(options, listOfActions.ToArray()); 

随着任务

由于您在这里使用任务,因此没有内置function。 但是,您可以使用我在博客上提供的那个。

  ///  /// Starts the given tasks and waits for them to complete. This will run, at most, the specified number of tasks in parallel. /// NOTE: If one of the given tasks has already been started, an exception will be thrown. ///  /// The tasks to run. /// The maximum number of tasks to run in parallel. /// The cancellation token. public static void StartAndWaitAllThrottled(IEnumerable tasksToRun, int maxTasksToRunInParallel, CancellationToken cancellationToken = new CancellationToken()) { StartAndWaitAllThrottled(tasksToRun, maxTasksToRunInParallel, -1, cancellationToken); } ///  /// Starts the given tasks and waits for them to complete. This will run, at most, the specified number of tasks in parallel. /// NOTE: If one of the given tasks has already been started, an exception will be thrown. ///  /// The tasks to run. /// The maximum number of tasks to run in parallel. /// The maximum milliseconds we should allow the max tasks to run in parallel before allowing another task to start. Specify -1 to wait indefinitely. /// The cancellation token. public static void StartAndWaitAllThrottled(IEnumerable tasksToRun, int maxTasksToRunInParallel, int timeoutInMilliseconds, CancellationToken cancellationToken = new CancellationToken()) { // Convert to a list of tasks so that we don't enumerate over it multiple times needlessly. var tasks = tasksToRun.ToList(); using (var throttler = new SemaphoreSlim(maxTasksToRunInParallel)) { var postTaskTasks = new List(); // Have each task notify the throttler when it completes so that it decrements the number of tasks currently running. tasks.ForEach(t => postTaskTasks.Add(t.ContinueWith(tsk => throttler.Release()))); // Start running each task. foreach (var task in tasks) { // Increment the number of tasks currently running and wait if too many are running. throttler.Wait(timeoutInMilliseconds, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); task.Start(); } // Wait for all of the provided tasks to complete. // We wait on the list of "post" tasks instead of the original tasks, otherwise there is a potential race condition where the throttler's using block is exited before some Tasks have had their "post" action completed, which references the throttler, resulting in an exception due to accessing a disposed object. Task.WaitAll(postTaskTasks.ToArray(), cancellationToken); } } 

然后创建任务列表并调用函数让它们运行,一次最多同时执行3个,你可以这样做:

 var listOfTasks = new List(); for (int i = 0; i < 10; i++) { var count = i; // Note that we create the Task here, but do not start it. listOfTasks.Add(new Task(() => Something())); } Tasks.StartAndWaitAllThrottled(listOfTasks, 3);