private static async Task SemaphoreExtensionsの実験() { Console.WriteLine(); Console.WriteLine("########## ########## ##########"); Console.WriteLine("SemaphoreExtensionsの実験"); { Console.WriteLine(); Console.WriteLine("排他処理しない場合"); var tasks = Enumerable.Range(0, 5).Select(t => Task.Run(async() => { Console.Write($"{t} -> "); await Task.Delay(100); Console.WriteLine($"<- {t}"); })); await Task.WhenAll(tasks); } { Console.WriteLine(); Console.WriteLine("排他処理する場合"); var s = new SemaphoreSlim(1); var tasks = Enumerable.Range(1, 5).Select(t => Task.Run(async() => { await s.Critical(async() => { Console.Write($"{t} -> "); await Task.Delay(10); Console.WriteLine($"<- {t}"); }); })); await Task.WhenAll(tasks); } }