The following works as expected when building in Debug – the execution is done after three seconds. But, for some reason, it fails when in Release.
class Program { static void Main(string[] args) { var w = new Worker(); while (!w.IsDone); Console.WriteLine("The work is done."); } } class Worker { public bool IsDone; public Worker() { var thread = new Thread(Job); thread.Start(); } private void Job() { Thread.Sleep(3000); IsDone = true; } }
Could you explain what is happening? How could you fix it?
Tomorrow, I will share the answer.