Consider this a huge pool of tasks:
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);
What if I only wanted to run say 3 tasks simultaneously? How would I implement that in code?
A less complicated example than the MSDN version would be to use Parallel.Invoke setting the max degree of parallelism:
Parallel.Invoke() will however block until all parallel operations are finished (meaning that no code beyond the parallel.invoke will run until they are all completed). If this doesn’t work for you then you will end up needing to create your own task scheduler as is shown is the MSDN article linked by Daniel.