2

I'm trying to learn C# threading, and I've been cautioned that threads that are not background and that are not explicitly terminated will continue running, and prevent your application from terminating. To test this I wrote a quick C# console application. The program contains a method with an infinite loop. The infinite loop method is called in its own thread from Main, and is never explicitly terminated. I also verify that the IsBackground property is set to false. So I expected that when I tried to terminate the console application by clicking the "X" on the console window that the program would hang, which it didn't. Also with the application running and looking at the task manager I see the application with 8 thread. When I close the program by clicking the "X", it is no longer in the Task Manger Processes tab, and I don't see any threads that obviously belong the the application still running in the process tab. So two questions: 1. Is that infinite loop thread still running after after I close the application, and why don't I see them in "Task Manager"? 2. Also as an aside, why do I see 8 threads running in the "Task Manager" for that process, when I expected only 2.

    class Program
    {
    static string backgroundsetting;

    public static void looper()
    {
        int i = 0;
        while (true)
        {
            Console.WriteLine(" looper int i = {0}", i);
            Console.WriteLine("Thread background property is ...." + backgroundsetting);
            i++;

        }
    }

    static void Main(string[] args)
    {
        Thread loopyThread = new Thread(looper);
        backgroundsetting = loopyThread.IsBackground.ToString();
        loopyThread.Start();        
    }
}
Frank
  • 459
  • 1
  • 5
  • 13
  • It's worth asking just a single question per post. The second part about running 8 threads is really an implementation detail - there are various reasons why extra threads might be running, such as garbage collection, or the thread pool. – Jon Skeet Apr 04 '18 at 16:47
  • Can't confirm the 8 threads. The only thing that uses a zillion threads on my machine is Firefox. ;-) – oliver Apr 04 '18 at 16:50
  • 1
    Possible duplicate of [When does a multithreaded console application exit?](https://stackoverflow.com/questions/14617258/when-does-a-multithreaded-console-application-exit) (via [Does a .NET application wait for all foreground threads to finish before it terminates the program?](https://stackoverflow.com/questions/37552601/does-a-net-application-wait-for-all-foreground-threads-to-finish-before-it-term)) – Lance U. Matthews Apr 04 '18 at 17:08
  • Threads live *within* processes. If the process is gone, so are *any* threads. Think of processes as a container for threads. And think of threads as something that cannot live outside of the container they were created in. – Damien_The_Unbeliever Apr 04 '18 at 17:11

2 Answers2

3

No, non-background threads don't prevent the process from being forcibly terminated. Instead, it's more that the CLR will terminate the process itself when the only threads running are background threads.

So non-background threads will keep the CLR running in normal situations, but they don't have any super-powers to prevent process shutdown.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1
  1. No, your infinite loop thread is not still running after your process exits. When the process exits all of its threads terminate execution as well. You wouldn't expect to see threads in the task manager without a process to which they belong. Since the process has exited, there is no entry in the task manager and no associated threads.
  2. You should see your main thread, your worker thread, the Garbage Collector thread, and the finalizer thread. You'll also have pooled threads for IO Completion ports because you are using the console. If you see more than that, then the other threads are related to the debugger.
Wyck
  • 10,311
  • 6
  • 39
  • 60