using System.Collections.Generic;
using System.Threading.Tasks;
int maxParallelTasks = 3;
SemaphoreSlim semaphore = new SemaphoreSlim(maxParallelTasks);
List<Task> tasks = new List<Task>();
for (int i = 0; i < 10; i++)
await semaphore.WaitAsync();
Task task = Task.Run(async () =>
Console.WriteLine($"Task {taskId} started " + semaphore.CurrentCount);
Console.WriteLine($"working {taskId} " + semaphore.CurrentCount);
Console.WriteLine($"Task {taskId} completed " + semaphore.CurrentCount);
await Task.WhenAll(tasks);
Console.WriteLine("All tasks completed");