From: Tony Johansson on
Hi!

Below is a really simple Thread example.
If I run this example in debug mode and set a breakpoint in the SimpleWork
why is not this row called immediately when this statement
theThread.Start(); is executed.
I have noticed that it is called first when I leave the main method

I have tested several thing when this breakpoint in SimpleWork is hit
1. If I for example add a read in the main then as soon as this read has
been executed this breakpoint in SimpleWork is hit
2. If I for example add a sleep on the main thread in main then this
breakpoint in SimpleWork is hit as soon as this sleep is executing
3.If I for example add a WriteLine in the main then as soon as this
WriteLine has been executed this breakpoint in SimpleWork is hit. This
happen even if I have some other statement after this WriteLine.

So my question is just what is it that cause the SimpleWork to be called. I
mean in example 1 I have to leave the main method before the SimpleWork was
called but in 2 I had to execute a readLine before the SimpleWork was called
and in exemple 3 I had to execute a WriteLine before the SimpleWork was hit

public static void Main()
{
ThreadStart operation = new ThreadStart(SimpleWork);
Thread theThread = new Thread(operation);
theThread.Start();
}

private static void SimpleWork()
{
Console.WriteLine("Thread: {0}", Thread.CurrentThread.ManagedThreadId);
}

//Tony