Determine number of threads waiting in threadpool in C#
c#, multithreading
Solution
There is a ThreadPool.PendingWorkItemCount property.
https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool.pendingworkitemcount?view=netcore-3.1
Another possibility if you're running .net core, is to run `dotnet counters monitor --process-id 123`
This will give you a console display of some runtime metrics. I believe the metric you're asking about is 3rd from the bottom.
Press p to pause, r to resume, q to quit.
Status: Running
[System.Runtime]
% Time in GC since last GC (%) 0
Allocation Rate / 1 sec (B) 66,480
CPU Usage (%) 0
Exception Count / 1 sec 0
GC Heap Size (MB) 234
Gen 0 GC Count / 60 sec 0
Gen 0 Size (B) 200,991,744
Gen 1 GC Count / 60 sec 0
Gen 1 Size (B) 385,440
Gen 2 GC Count / 60 sec 0
Gen 2 Size (B) 210,807,272
LOH Size (B) 74,140,000
Monitor Lock Contention Count / 1 sec 0
Number of Active Timers 314
Number of Assemblies Loaded 272
ThreadPool Completed Work Item Count / 1 sec 38
ThreadPool Queue Length 0
ThreadPool Thread Count 9
Working Set (MB) 242
Some useful links:
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-counters https://www.youtube.com/watch?v=jn54CjePzs0
Problem
Is it possible to see how many threads are waiting to execute in the queue of the Threadpool? If I do this: ``` for (int i = 0; i < 5000; i++) { tasks.Add(Task.Run(() => { TaskTest(); })); } Task.WaitAll(tasks.ToArray()); ``` Inside TaskTest, I do some work that typically takes ~500ms, I also update a variable with the highest number of threads seen: ``` ThreadPool.GetMaxThreads(out maxthreads, out completionthreads); ThreadPool.GetAvailableThreads(out availablethreads, out completionthreads); int threads_in_use = maxthreads - availablethreads; mHighwaterThreads = Math.Max(mHighwaterThreads, threads_in_use); ``` Max threads, for my computer, is usually above 32000. mHighwaterThreads is usually around 75. I would like to know if there is a way to see how many threads are waiting to execute in the Threadpool.