Thread-safe programming techniques
Look at this piece of code. Run it and see what happens:
using ExtensionLibrary; int iterationCount = 100; ThreadPool.QueueUserWorkItem(async (state) => { await Task.Delay(500); iterationCount = 0; $"We are stopping it...".Dump(ConsoleColor.Red); }); await WaitAWhile(); $"In the main part of the app.".Dump(ConsoleColor.White); "Main app is done.\nPress any key to stop.".Dump(ConsoleColor.White); Console.ReadKey(); return 0; async Task WaitAWhile() { do { $"In the loop at iterations {iterationCount}". Dump(ConsoleColor.Yellow); await Task.Delay(1); }while (--iterationCount > 0) ; }
We have a Task...