Well, I'm not a C# developer, but it's a common concept among programming field that the main()
of certain program runs in one specific thread (the main thread) and in the same way other threads just exits when their work is done and aren't in a infinite loop situation, the main thread finishes as well, regardless any other thread running parallel to it. Because of that when the main thread ends you won't receive any output from any other thread since the program main output is tied to the main thread (the one you started when called the program).
The correct thing to do is as @Derek stated before: "join all of the threads" before main thread termination. The join()
acts like a semaphore to the main thread when called, i.e.: for each .join()
called to on a specific thread, the main function must wait for that specific thread to finish.
t1.Join();
t2.Join();
t3.Join();
forces main thread to wait t1, t2 and t3. The problem here is that you create anonymous threads, so there isn't a simple way to refer to them.
As I said earlier: I'm not a C# developer, so I don't really know what would be the best way possible to handle it (maybe through a pool of threads?), but the aforementioned solution of adding Console.ReadLine()
might solve the problem, even being not related to the other threads at all, it's just a 'trick' to make main thread wait something (an input from the user).
The idea of my entire post is to warn you about the existence of a main thread
that doesn't care or know about other threads running in parallel if you doesn't explicitly notifies it (through .join()
).