Running X Tasks at a time using Semaphore


The following illustrates how to run X tasks at a time until all tasks are completed
// declare a variable to hold how many tasks to run simultaneously.
int maxBatchCount = 5;

// Create an instance of a SemaphoreSlim object with a parameter to 
// indicate how many tasks should be allowed to run at the same time.
var maxThread = new SemaphoreSlim(maxBatchCount);

// Get an enumerable list of our properties that we want to process
// We want to process all of these, but only allow a certain number of tasks to run at the same time.
IEnumerable propertyList = this.GetPropertiesForProcessing();

// select each of the items in our enumerable list and process each accordingly
IEnumerable<Task> propertyTasks = propertyList.Select(async property =>
{
    // call the WaitAsync method on the Semaphone object.
    // if there are already the max number of threads running, the process will wait here.
    await maxThread.WaitAsync();
    
    try
    {
        // do some work on our property
        await this.DoPropertyWorkAsync(property);
    }
    finally
    {
        // release our semaphore (decrement the counter)
        maxThread.Release();
    }
}

// Wait for all of our property tasks have completed.
// Note: this is a BAD way to do this. See note below.
var allTasks = await Task.WhenAll(createBatchTasks);


Related


Last modified by Mohit @ 4/5/2025 9:06:22 PM