I faced with some problem I cannot understand why it is happening. Here is simple example:
class ConsoleApp
{
static void Main(string[] args)
{
Thread workThread = new Thread(ThreadProc);
//Console.WriteLine("Starting"); // uncomment this
workThread.Start();
Console.ReadKey(true); // first ReadKey
Console.ReadKey(true); // second ReadKey
}
static void ThreadProc()
{
Console.WriteLine("ThreadProc started");
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
int timeout = rnd.Next(500, 1000);
Thread.Sleep(timeout);
Console.WriteLine("ThreadProc {0} slept {1} ms", i, timeout);
}
}
}
When I run this, workThread doesn't start until I press some key (after first ReadKey). If I uncomment first Console.WriteLine, workThread starts immediately.
Can anyone explain this behavior?