Run a given number of the same process concurrently

backgroundworker, c#, multithreading

Solution

Consider the following code:

private async void CodeOnUiThread()
{
    //do ui stuff before starting
    await ExecuteProcesses();
    //do ui stuff after completing.
}

private async Task ExecuteProcesses() 
{
    await Task.Factory.StartNew(() =>
    {
        List<string> myStrings = GetMyStrings(); //or whatever you need
        Parallel.ForEach(myStrings,
            new ParallelOptions()
            {
                MaxDegreeOfParallelism = 4
            }, (s) =>
            {
                var process = new Process();
                process.StartInfo = new ProcessStartInfo("myProcess.exe", s);
                process.Start();
                process.WaitForExit();

            });
    }); 
}

This allows a maximum of 4 threads to run simultaniously, thus not allowing more than 4 processes to execute at the same time.

Update:

You can also use `Environment.ProcessorCount` to get the number of cores. However the `Parallel.ForEach` call will handle this correctly by default.

Update 2

`Parallel.ForEach` will block the thread currently running. I have updated the code above.

Problem

My situation is simple but complex. I am trying to write a program, that needs to execute an external process 1100 times, 4 at a time. I am completely stumped on how to go about this. The application I am writing is a "Windows Form Application", and I am utilizing BackgroundWorker to run the tasks async. Example, I have a list of 1100 different strings, and I want to run the process 1 time per string, but only 4 at a time, and then move on to the next 4. Any help would be appreciated.

Original source