Click here to Skip to main content
15,795,318 members
Articles / General Programming / Threads

Throttling Multiple Tasks to Process Requests in C#

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
31 Oct 2018CPOL2 min read 25.2K   362   17   6
Extension method to throttle multiple Tasks in C#


Task Parallel Library is a powerful feature introduced by Microsoft as part of the .NET Framework. This acts as a wrapper to existing ThreadPool which is now managed by CLR optimally. This saves us from the lengthy boilerplate code of maintaining and disposing the threads. There are many articles that cover the advantages of TPL and its advantages over traditional thread pool.

Here is a very good article that gives a detailed overview of TPL.

This is a short tip that will cover a common use case while using TPL: Limiting the number of tasks that execute in parallel.


We use Task Parallel Library when processing multiple requests. A service or a worker process can load a set of requests and process all of them parallelly using TPL. This is a very common application of TPL, where a windows service application polls for any new requests and processes them in parallel. In some cases, we might want to control the number of concurrent processes.

About the Code

Attached is a .NET 4.5 Console application with structural methods that make use of the extension method.

He have a method fun() that takes the processId as parameter and sleeps for some random time. This will simulate over async task.

public async Task fun(int processId)
           await Task.Run( () =>{
               Random rand = new Random();
               Console.WriteLine("Processing " + processId);
               Console.WriteLine("Done processing - " + processId);

Extension Method

public static async Task executeParallel<T>
      (this IEnumerable<T> items, int limit, Func<T, Task> action)
         var allTasks = new List<Task>(); //Store all Tasks
         var activeTasks = new List<Task>();
         foreach (var item in items)
             if (activeTasks.Count >= limit)
                 var completedTask = await Task.WhenAny(activeTasks);
             var task = action(item);
         await Task.WhenAll(allTasks); // Wait for all task to complete

items denote the list of input arguments for our parallel process. In other words, we are iterating over the pending requests in the queue and throttling will be handled by the logic inside the executeParallel method.

The extension method will be called as follows:

await queue.executeParallel(limit, fun); 

Main Method

The main method will take an input of number of processes. We can see the throttle in work.

static void Main(string[] args)
      List<int> queue = new List<int>();
      ProcessingService service = new ProcessingService();
      int n, limit = 3 ; // any arbitrary limit     
        Console.WriteLine("Enter number of process to Add: ");
        n = Convert.ToInt32(Console.ReadLine());
        for(int processid = 0; processid <= n; processid++)


Throttling in Action

We can see the next process being picked up automatically and at any time, there will be at most 3 processes running.


  • 31st October, 2018: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United States United States
A Full-Stack Web Developer and UI/UX enthusiast.

Comments and Discussions

SuggestionNot a robust solution Pin
tyrotoxin4-Nov-18 20:47
tyrotoxin4-Nov-18 20:47 
QuestionInbuilt Framework options Pin
Graeme_Grant31-Oct-18 17:08
mvaGraeme_Grant31-Oct-18 17:08 
QuestionUsing the SemaphoreSlim class is a good alternative choice Pin
George Swan31-Oct-18 13:18
mveGeorge Swan31-Oct-18 13:18 
AnswerRe: Using the SemaphoreSlim class is a good alternative choice Pin
Graeme_Grant31-Oct-18 16:59
mvaGraeme_Grant31-Oct-18 16:59 
GeneralRe: Using the SemaphoreSlim class is a good alternative choice Pin
George Swan31-Oct-18 22:17
mveGeorge Swan31-Oct-18 22:17 

Thanks for the information, Graeme. The sample app is using C# version 7.0 and async Main is not implemented. The feature is available from version 7.1 and is a most welcome addition.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.