Skip to content

Instantly share code, notes, and snippets.

Created August 28, 2017 10:19
Show Gist options
  • Save anonymous/e599471720ed4b4e694800354ab8e907 to your computer and use it in GitHub Desktop.
Save anonymous/e599471720ed4b4e694800354ab8e907 to your computer and use it in GitHub Desktop.
Example of controlled tasks creation
class Program
{
public static int i;
static void Main(string[] args)
{
SemaphoreSlim sem = new SemaphoreSlim(15, 15);
int amountOfWork = 2000;
while (amountOfWork > 0)
{
sem.Wait();
Task.Factory.StartNew(() => DoJob(sem));
amountOfWork -= 1;
}
Console.Read();
}
public static void DoJob(SemaphoreSlim sem)
{
Interlocked.Increment(ref i);
Console.WriteLine(i);
Thread.Sleep(1500);
sem.Release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment