Running 30 instances of Task at the same time

Here is the code to run 30 instances of Task

  int total = socketCount;
 int perRun = 30;

 if(perRun > total)
 {
 perRun = total;
 }

 int currentLoad = 0;
 int index = 0;

 var ts = new Task[perRun];
 int taskIndex = 0;
 while (true)
 {
 while (currentLoad < perRun)
 {
 if (index < total)
 {
 ts[taskIndex] = OpenWebSocketsSlice(index,webSockets[index++]);
 currentLoad++;
 taskIndex++;
 }
 else
 {
 break;
 }
 }

 taskIndex = Task.WaitAny(ts);
 currentLoad--;


 if (currentLoad == 0)
 {
 Task.WaitAll(ts);
 break;
 }
 }


 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑