Why 3 threads for a basic single threaded c# console app? Why 3 threads for a basic single threaded c# console app? multithreading multithreading

Why 3 threads for a basic single threaded c# console app?


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.


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


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