What is the problem of the following code? Task.Factory.StartNew(() => { while (true) { Console.WriteLine("@ " + DateTime.Now.ToString()); Thread.Sleep(TimeSpan.FromSeconds(2)); } }); var task = Task.Factory.StartNew(() => { while (true) { Console.WriteLine("! " + DateTime.Now.ToString()); Thread.Sleep(TimeSpan.FromSeconds(4)); } }); Task.WaitAll(task); The main problem is related to Thread.Sleep. Using this method in combination with tasks we can gen odd behavior and in the end the performance of the tasks can be affected. One of the behavior that we could get is related to how the sleep period – that will not be the same as we expect ("random" sleep interval). This occurs because of the TPL behavior, which will not guarantee that each task will have a dedicated thread where he can run. What we should do to resolve this problem? We have two possible solutio...
DREAMER, CRAFTER, TECHNOLOGY ENTHUSIAST, SPEAKER, TRAINER, AZURE MVP, SOLVING HARD BUSINESS PROBLEMS WITH CUTTING-EDGE TECHNOLOGY