Andrey Rubshtein Andrey Rubshtein - 1 year ago 43
C# Question

Can I limit the amount of System.Threading.Tasks.Task objects that run simultaneously?

I am using Task classes to do multi-threaded computation.

For example:

List<Task> taskList = new List<Task>();
for(int i=0;i<10;i++){
var task = new Task(() => Console.WriteLine("Hello from taskA."));
taskList.Add(task);
task.Start();
}


Is there a way to make only, let's say, 3 tasks to run at most, and the rest to wait?

Answer Source

change the MaxDegreeOfParallelism property.

example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download