Sorry if my question seems naive. I'm new to C# and .Net, and still wrap my head around them. I come from a Go-Lang background, and try to learn C#/.Net multithreading.
In Go the main thread will run and complete its logic regardless of other threads if no wait is used. I thought it should be the same in C#, however, the code below allow all threads to run completely. Which means Main thread waits other threads to complete, without using join() or any other wait techniques. Could you please, let me know what I missed here or misunderstood.
namespace TestThread
{
internal class Program
{
static void Main(string[] args)
{
Thread T1 = new Thread(PrintY);
T1.Start();
// The following is the funtion of the Main thread.
for (int i = 0; i < 10; i++) Console.Write("x");
}
static void PrintY()
{
for (int i = 0; i < 100; i++)
{
Console.Write("Y");
Thread.Sleep(100);
}
}
}
}
The output is like the following:
xxxxxxxxxYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
I expected at most one Y
in the results before the Main
method finishes and therefore the process terminates. What is keeping the process alive when the main thread is completed?