37

This simple program starts with 15 threads - according to the count. Sometimes during its lifetime it drops a few, but they come back.

class Program
 {
     static void Main(string[] args)
     {
         while (true)
         {
             Console.WriteLine(Process.GetCurrentProcess().Threads.Count);
             Thread.Sleep(500);
         }
     }
 }

I was expecting the process to just have one thread (and my intuition was backed up by this)

Without the debugger, the process has only (!) 4 threads. Surely any CLR stuff would be hidden from my process?

What count is this? Does the process really have that many threads? Why?

Matt Jacobsen
  • 5,724
  • 4
  • 34
  • 46
  • just out of curiosity - who do I upvote here? everyone's got the same answer :D – obelix Aug 13 '10 at 13:08
  • 1
    @obelix: if they all deserve an upvote, you could upvote them all. Your kind of question should be asked on MSO. – Richard Aug 13 '10 at 13:12
  • @obelix: they all make sense so they've all got an upvote from me. When I'm back in the office on Monday I'll check up a few details and give the most detailed, backed up answer the trophy. – Matt Jacobsen Aug 13 '10 at 18:25
  • 2
    @Richard - did not think it merited a full blown discussion on meta. ergo just a comment. – obelix Aug 13 '10 at 20:23
  • My console application has 16 (!!!) threads. Could it be because I'm using LINQ? – Kees C. Bakker Dec 11 '12 at 10:36

4 Answers4

35

Try running it outside the debugger (i.e. press Ctrl+F5 instead of F5). You should only see three threads - the main thread, the GC thread & the finalizer thread IIRC. The other threads you see are debugger-related threads.

Phil Devaney
  • 17,097
  • 6
  • 40
  • 31
  • Yes, main thread, GC, and finalizer are exactly what you will see. However you need to A) run outside the debugger and B) turn off Visual Studio Hosting Process which adds a thread or two. – Tergiver Aug 13 '10 at 18:07
  • do you have a link to an msdn article? – Matt Jacobsen Aug 13 '10 at 18:22
  • 1
    is the VS hosting process activated even if I'm in Release mode without running the process through VS? I still see 4 threads... – Matt Jacobsen Aug 16 '10 at 09:46
  • 2
    Thread 4 might be a ThreadPool thread for any asynchronous work that needs to be done. Just guessing, though. – Sander Aug 17 '10 at 05:40
  • 1
    Need an evidence, It is completely wrong, I have seen the process moves between 4-7 threads outside the debugger!!! it is a very simple console app with an infinite loop! – Sawan Nov 14 '12 at 12:13
5

If you run it without a debugger attached, there are significantly fewer threads. And those would presumably be the finalizer thread, and other house-keeping CLR stuff.

Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
Kent Boogaart
  • 170,894
  • 35
  • 387
  • 389
5

Project + Properties, Debugging, untick "Enable the Visual Studio hosting process". I can't discover what it is doing. As soon as I tick the "Enabled unmanaged code debugging" option to try to get a peek at these threads, they no longer get started. No clue. But I'm sure it's for our benefit :)

Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455
  • 1
    I think the hosting process gives the debugger go faster stripes, or something like that. – Matt Jacobsen Aug 13 '10 at 18:26
  • 1
    I beleive the hosting process was designed to give faster startup times for debugging - when you debug, the relevant assemblies get loaded into the vshost process, rather than the debugger having to keep on creating/destroying new processes. – Alex Humphrey Aug 14 '10 at 10:24
  • 2
    Nah, the hosting process is a custom hosted version of the CLR. Mostly for security purposes. – Hans Passant Aug 14 '10 at 12:01
4

Try running it without the debugger (Visual Studio) attached (Ctrl+F5). You'll see that there will be less (probably 3) threads. Most of those threads have to do with the debugger.

BFree
  • 100,265
  • 20
  • 154
  • 199