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?
Threads are tasks that can run concurrently to other threads and can share data. When your program starts, it creates a thread for the entry point of your program, usually a Main function. So, you can think of a "program" as being made up of threads.
If your ThreadPool runs out of threads two things may happen: All opperations are queued until the next resource is available. If there are finished threads those might still be "in use" so the GC will trigger and free up some of them, providing you with new resources.
32767 in Framework 4.0 (64-bit environment)
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With