Created
August 28, 2017 10:19
-
-
Save anonymous/e599471720ed4b4e694800354ab8e907 to your computer and use it in GitHub Desktop.
Example of controlled tasks creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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