I am using this code snippet below to list the threads in a basic console app.
lock (threadLock)
{
ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;
foreach (ProcessThread thread in currentThreads)
{
Console.WriteLine(" Id={0}, CurrentPriority={1}, State={2}",
thread.Id, thread.CurrentPriority, thread.ThreadState);
}
}
There are six threads. Below, is the initial output.
Id=2924, CurrentPriority=8, State=Running
Id=8900, CurrentPriority=8, State=Wait
Id=524, CurrentPriority=8, State=Wait
Id=4916, CurrentPriority=8, State=Wait
Id=5124, CurrentPriority=8, State=Wait
Id=6780, CurrentPriority=11, State=Wait
Apparently, the first thread is the main thread. The last one, with CurrentPriority=11, is the Finalizer thread because below is the output when run inside a destructor:
Id=2924, CurrentPriority=8, State=Wait
Id=8900, CurrentPriority=8, State=Wait
Id=524, CurrentPriority=8, State=Wait
Id=4916, CurrentPriority=8, State=Wait
Id=5124, CurrentPriority=8, State=Wait
Id=6780, CurrentPriority=11, State=Running
My question is about the other four threads. One of them is supposed to be UI thread. Another is supposed to be a Garbage Collector (GC) thread. Apparently, one is supposed to be a Threadpool thread. What is the last sixth thread? What are the code snippets for each one so that I can run each individual thread? Does GC thread ever execute application code? A lot of sources mix up Finalizer and GC threads so it is hard to figure out whether these are seperate threads or one thread. It would be helpful to have code snippets for each one so that I can actually run each thread.