19

I created a console app in c# with a single Console.ReadLine statement. Running this app within Visual Studio and stepping into the debugger shows 7 threads in the thread window (6 worker threads, one is .NET SystemEvents and the other is vshost.RunParkingWindow and the main thread).

When I run the app outside Visual Studio I see a total of 3 threads in Windows task manager. Why so many when I would expect 1 thread? What are the others being spawned for?

1
  • I would assume executing the .NET runtime is what they're for, but I cannot back that assertion up with any specific knowledge. Apr 17, 2009 at 21:48

5 Answers 5

38

If you're running a .NET application, I believe you always get a thread (mostly sleeping) for the JIT (Just-in-Time compiler) as well as the GC (Garbage Collection) thread, in addition to your main thread.

6

You do not need to worry: If you don't explicitly use them you won't have any of your code running in another thread than the main thread. The other threads are for:

  • Garbage collector
  • Finalization
  • Threadpool

Do the 3 threads share one stdin?

Theorethically yes, but the others won't use it unless you use Console.ReadLine inside a destructor or inside ThreadPool.QueueUserWorkItem, so don't worry you will get all data in main thread

3

There is a way to move SystemEvents notifier into your thread:

public static class ThreadingHelper_NativeMethods
{
   [DllImport("user32.dll")]
   public static extern bool IsGUIThread(bool bConvert);
}


     // This code forces initialization of .NET BroadcastEventWindow to the UI thread.
     // http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/fb267827-1765-4bd9-ae2f-0abbd5a2ae22
     if (ThreadingHelper_NativeMethods.IsGUIThread(false))
     {
        Microsoft.Win32.SystemEvents.InvokeOnEventsThread(new MethodInvoker(delegate()
        {
           int x = 0;
        }));
     }
1

Don't forget that finalizer thread !

3
  • There is no such thing as a finalizer thread; however, there is a GC thread.
    – Samuel
    Apr 17, 2009 at 22:07
  • 1
    @Samuel : the full name is in fact the GC Finalizer Thread, or at least that's what google seems to say. the Accepted Answer has been edited during the 5mn initial timeframe to add GC, which it doesn't in its original version, thus my answer.
    – Brann
    Apr 18, 2009 at 7:33
  • Uh, yes there is a finalizer thread. It runs at low priority in the background to process a queue that the GC sets up. The GC itself does not necessarily run in a separate thread from your application.
    – Promit
    Apr 28, 2009 at 16:37
1

If you've not done any threading or threadpool work, then only one of those threads is yours. The rest are created by the runtime and it's libraries e.g. garbage collection. The number of threads will vary depending on the libraries you use, the number of cores etc.

Those threads may share context e.g. Console, but you only need to worry about synchronizing your own threads and constructs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.