1

As far as I understand, .NET CLR creates a thread pool for each process. So each process has its own thread pool. And in every thread pool, there exist a certain number of threads available. It might be increasd or decreased as deemed necessary by the framework, but it starts with a predetermined number of threads for each process.

I wanted to find out the number of threads it will start with for a simple WPF application. When I used the System.Threading.ThreadPool.GetMaxThreads(out worker, out io) and System.Threading.ThreadPool.GetAvailableThreads(out worker, out io), I got the same result of 2047 worker threads and 1000 io threads. But I assume this can't be right, so this is not the right way to find the currently reserved threads in the thread pool.

So I looked at the thread count using Windows Task Manager and it showed 10 threads for the application. That seemed sensible and I came to the conclusion that the thread pool has 9 threads since one of the 10 is the main UI thread.

First of all, is my conclusion of 9 threads in thread pool correct? Second, what is the right way of querying it using c#?

John L.
  • 1,825
  • 5
  • 18
  • 45
  • Why do you need that at all? Especially in a WPF app. – dymanoid Oct 30 '18 at 13:24
  • 1
    ThreadPools are there so you do not ever **need** to micromanage threads. If you lacked them, you would either have to hardcode the amount of Threads or write code to run different amounts based on if you run on a 2 Core Tablet or 2x64 core Server. – Christopher Oct 30 '18 at 13:30
  • You also might have a missunderstanding between "Queing", "Active" and "Currently Executed" Tasks. As I understand it the step from "Qued" to "Active" is something that happens way before execution can even begin. There is also a chacne that you miss-read the Task Manager. If those threads are executed really fast, they might have all been done before TaskManager even updated the thread count. – Christopher Oct 30 '18 at 13:32
  • Consider this sequence for a example: 1 Task is queued. It is picked up by a PoolThread, changing it to active. The Task then is stuck (waiting for a lock to be aquired, yielding intentionally as it waits for some external resource). So now the task is Active. But not actually being executed. With 1000 Tasks, it is easily possible that all of them had been started only to be stuck and waiting for a exclusive resource now. – Christopher Oct 30 '18 at 13:42
  • Possible duplicate of [How to count the amount of concurrent threads in .NET application?](https://stackoverflow.com/questions/15381174/how-to-count-the-amount-of-concurrent-threads-in-net-application) – Fildor Oct 30 '18 at 13:58
  • See also: [PerformanceCounter Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.performancecounter?view=netframework-4.7.2) and [Lock and thread performance counters](https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/performance-counters#lockthread) – Fildor Oct 30 '18 at 14:03
  • 2
    WPF always has 2 threads, so there would be 8 in the pool - same as your number of cores. All is well and at rest. – bommelding Oct 30 '18 at 14:09
  • 2
    Task Manager shows you too much, you also see unmanaged threads. Like those used by unmanaged threadpool that Win10 uses, the finalizer thread, possibly the debugger thread and the thread that makes Timers tick. Threadpool starts out with Environment.ProcessorCount threads that can do work. If you use Win10 then that's likely to be 4. You never want to get close to the GetMaxThreads() value. Use Debug > Windows > Threads to see what is really going on. – Hans Passant Oct 30 '18 at 14:35

0 Answers0