I was just starting out on LinqPad and trying some thread snippets in it and I was bewildered as to why my code is not doing as predicted to do.
Thread t1 = new Thread
(delegate()
{
for (int cycles = 0; cycles < 1000; cycles++)
{
Thread.Sleep(500);
Console.WriteLine("Hello World!");
}
}
);
t1.Start();
Console.WriteLine("Soham");
Why is this only printing Soham
. The code block inside the thread is not at all executing. I'm not able to understand why because the syntax compiles fine and as far as I I know about c# this should compile fine and run in VS2010 and execute both outputs, even though the order of which cannot be determined. What am I doing or thinking wrong here. I may need some useful tutorials and suggestions to get used to LinqPad.
Try adding a t1.Join()
after the Console.WriteLine("Soham")
:-) The LINQPad probably sees the main thread terminating and kills everything. With the t1.Join();
the main thread will wait for the other thread to finish.
Ah... and just tested it :-)
I'll add that you can write in less characters:
new Thread(() =>
{
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With