Executes a trace on the ThreadPool for all callbacks currently queued. This will not include work items that have a time delayed callback.
This class heavily relies on reflection to get the ThreadPool queue. Therefore it is very unlikely to work in MONO and can break if Microsoft changes any of the member names or how the ThreadPool works. In this case WorksInThisRuntime will be set to false and GetTrace will return "Not Supported"
Example #1
0
        private static void MonitorThreads()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("--------------------------------------------------------");
            sb.AppendLine("Thread Pool Queue");
            sb.AppendLine("--------------------------------------------------------");

            ThreadPoolTrace.GetTrace(sb);

            sb.AppendLine("--------------------------------------------------------");
            sb.AppendLine("Timers");
            sb.AppendLine("--------------------------------------------------------");

            TimerTrace.GetTrace(sb);

            //Since current builds of openPDC appears to be doing better about not blocking all threads on the threadpool
            //this diagnostics functionality has been disabled but the code has been left in place if it's needed in the future.

            //            if (OptimizationOptions.EnableThreadStackDumping)
            //            {
            //#if !MONO
            //                using (var dataTarget = DataTarget.AttachToProcess(Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive))
            //                {
            //                    foreach (var clr in dataTarget.ClrVersions)
            //                    {
            //                        var runtime = clr.CreateRuntime();

            //                        sb.AppendLine("--------------------------------------------------------");
            //                        sb.AppendLine("Thread Stacks");
            //                        sb.AppendLine("--------------------------------------------------------");
            //                        foreach (var t in runtime.Threads)
            //                        {
            //                            bool hasWrittenData = false;

            //                            foreach (var item in t.StackTrace)
            //                            {
            //                                if (item.Method != null)
            //                                {
            //                                    if (!hasWrittenData)
            //                                        hasWrittenData = true;
            //                                    sb.AppendLine(item.ToString());
            //                                }
            //                            }
            //                            if (hasWrittenData)
            //                            {
            //                                sb.AppendLine("--------------------------------------------------------");
            //                            }
            //                        }
            //                    }
            //                }
            //#endif
            //            }
            Log.Publish(MessageLevel.Warning, "ThreadPool Stack Trace", "Dumped threadpool stack trace", sb.ToString());
        }