-1

Sorry for asking this rather silly question but I made this program to test whether or not all foreground threads are awaited for their completion before the program terminates.

But in this program, as soon as I hit any key to exit the program, the main thread terminates and then closes the application even while in the middle of executing the other foreground thread.

using System;
using System.Threading;

namespace ForegroundThreads
{
    // This is a program to test whether or not the application
    // will terminate when there is a pending foreground thread running.

    // In describing the difference between foreground and background threads, 
    // the documentation states that an application will terminate all
    // background threads when all foreground threads have finished execution.
    // This implies that the application will stall the main thread until
    // all foreground threads have completed execution. Let us see if this
    // is the case.

    // Quote:
    // A managed thread is either a background thread or a foreground thread. 
    // Background threads are identical to foreground threads with one exception: 
    // a background thread does not keep the managed execution environment running. 
    // Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), 
    // the system stops all background threads and shuts down. 
    // Source: https://msdn.microsoft.com/en-us/library/h339syd0%28v=vs.110%29.aspx

    class Program
    {
        static void Main(string[] args)
        {
            var t = new Thread(() => { 1000000.Times(() => { Console.WriteLine("Hello"); }); });
            t.Start();

            Console.WriteLine("Press any key to exit the main thread...");
            Console.ReadKey();
        }
    }

    public static class Extensions
    {
        public static void Times(this int numTimes, Action action)
        {
            for (int i = 0; i < numTimes; i++, action()) ;
        }
    }
}

The Behavior I Notice When I Run This Code

On my machine, if I reduce the number of times to a smaller value, say, 1000, it immediately kills all foreground threads when the main thread exits. But if I make the value large, like one million, for instance, the system keeps running that foreground thread I created disregarding all keystrokes until it has finished printing one million times.

Update

GSerg linked to another question that asks the same thing. However, if you read that question closely, the poster of that question is really asking, "What's going on?"

The answer simply quotes the MSDN explaining that all foreground threads are awaited for. My question contends the very thing.

My question is -- why is it that the program waits for the foreground thread to finish sometimes and it doesn't at other times. Hence, that answer in that other question is of no help to me.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • I cannot reproduce. If I keep the thread as foreground, the program doesn't terminate when I press enter. If I set its IsBackground property to true, the program does terminate. – xofz May 31 '16 at 18:14
  • Try changing the value of the receiver of the `Times` method. I am updating the question with more details as to when it waits for the foreground thread to finish and when it doesn't. On my machine, if I reduce the number of times to a smaller value, say, 1000, it immediately kills all foreground threads when the main thread exits. But if I make the value large, like one million, for instance, the system keeps running that foreground thread I created disregarding all keystrokes until it has finished printing one million times. – Water Cooler v2 May 31 '16 at 18:17
  • Using a Thread.Sleep() instead of Console.Writeline() might be more reliable. – Thomas Weller May 31 '16 at 18:20
  • Thanks, @ThomasWeller. That is a good suggestion. – Water Cooler v2 May 31 '16 at 18:22
  • With your "final update" it is not excatly clear what your question is anymore. – Scott Chamberlain May 31 '16 at 19:13
  • @ScottChamberlain Thank you. You're right. The question does not remain any longer a question. It may be closed. I don't want to delete it as I want to keep a record of it in case I forget. – Water Cooler v2 May 31 '16 at 19:26
  • Instead of closing, move FINAL UPDATE to an answer and accept the answer in two days so it does not get auto-promoted as a question with no accepted answer. – Scott Chamberlain May 31 '16 at 19:32
  • @ScottChamberlain Thank you for the suggestion. I have done so. – Water Cooler v2 May 31 '16 at 19:35

1 Answers1

0

My bad, my eyes were not seeing 20-20. The program does indeed wait for the spawned foreground thread to complete execution.

What was happening was that when I reduced the number of iterations by decreasing the value of the integer receiver of the Times method, even when the program actually had finished printing all the Hello's, it appeared to my eyes that the program was still busy printing. That led me to believe that the spawned thread is still running its course, and when I pressed any other key on my keyboard, it immediately terminated the process.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336