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();
}
}