Your "error" in thinking is, that you assume that the threads start synchronously in the order you create your program classes.
In fact you only "delegate" to the system, that you want to start a thread, and immediately a second thread.
There is simply no guarantee that the first thread will be up and running before the second thread if you do it this way.
There are lots of ways to synchronize that.
For this small demo, I show you a secret :)
A Thread has a "
ThreadState
" property where you can see, if it is running.
So, if you just put that little
while
... between your two thread starts, you force the program to wait until the first thread is in running state, before the second thread gets started.
Then you will always have the hello,hello,james bond output.
Program objProg1 = new Program("James");
Program objProg2 = new Program("Bond");
objProg1.myThread.Start();
while (objProg1.myThread.ThreadState != ThreadState.Running) ;
objProg2.myThread.Start();
Console.ReadLine();
Kind regards, Mike