private void OnPerformanceTimerCallback(object state)
        {
            int availableWorkingThreads, availableCompletionPortThreads;

            ThreadPool.GetAvailableThreads(out availableWorkingThreads, out availableCompletionPortThreads);

            int maxWorkingThreads;
            int maxCompletionPortThreads;

            ThreadPool.GetMaxThreads(out maxWorkingThreads, out maxCompletionPortThreads);

            NodeSummary globalPerfData = null;

            var retry = false;

            while (true)
            {
                try
                {
                    globalPerfData = new NodeSummary
                    {
                        AvailableWorkingThreads        = availableWorkingThreads,
                        AvailableCompletionPortThreads = availableCompletionPortThreads,
                        MaxCompletionPortThreads       = maxCompletionPortThreads,
                        MaxWorkingThreads = maxWorkingThreads,
                        CpuUsage          = m_CpuUsagePC.NextValue() / m_CpuCores,
                        TotalThreadCount  = (int)m_ThreadCountPC.NextValue(),
                        WorkingSet        = (long)m_WorkingSetPC.NextValue()
                    };

                    break;
                }
                catch (InvalidOperationException e)
                {
                    if (retry)
                    {
                        throw e;
                    }
                    if (Environment.OSVersion.Platform == PlatformID.Unix ||
                        Environment.OSVersion.Platform == PlatformID.MacOSX)
                    {
                        throw e;
                    }
                    var newInstanceName = GetPerformanceCounterInstanceName(Process.GetCurrentProcess());
                    SetupPerformanceCounters(newInstanceName);
                    retry = true;
                }
            }

            var perfBuilder = new StringBuilder();

            perfBuilder.AppendLine("---------------------------------------------------");
            perfBuilder.AppendLine(string.Format("CPU Usage: {0}%, Physical Memory Usage: {1:N}, Total Thread Count: {2}", globalPerfData.CpuUsage.ToString("0.00"), globalPerfData.WorkingSet, globalPerfData.TotalThreadCount));
            perfBuilder.AppendLine(string.Format("AvailableWorkingThreads: {0}, AvailableCompletionPortThreads: {1}", globalPerfData.AvailableWorkingThreads, globalPerfData.AvailableCompletionPortThreads));
            perfBuilder.AppendLine(string.Format("MaxWorkingThreads: {0}, MaxCompletionPortThreads: {1}", globalPerfData.MaxWorkingThreads, globalPerfData.MaxCompletionPortThreads));

            Logger.LogDebug("Performance:" + perfBuilder.ToString());
        }
        private void OnPerformanceTimerCallback(object state)
        {
            int availableWorkingThreads, availableCompletionPortThreads;
            ThreadPool.GetAvailableThreads(out availableWorkingThreads, out availableCompletionPortThreads);

            int maxWorkingThreads;
            int maxCompletionPortThreads;
            ThreadPool.GetMaxThreads(out maxWorkingThreads, out maxCompletionPortThreads);

            NodeSummary globalPerfData = null;

            var retry = false;

            while (true)
            {
                try
                {
                    globalPerfData = new NodeSummary
                    {
                        AvailableWorkingThreads = availableWorkingThreads,
                        AvailableCompletionPortThreads = availableCompletionPortThreads,
                        MaxCompletionPortThreads = maxCompletionPortThreads,
                        MaxWorkingThreads = maxWorkingThreads,
                        CpuUsage = m_CpuUsagePC.NextValue() / m_CpuCores,
                        TotalThreadCount = (int)m_ThreadCountPC.NextValue(),
                        WorkingSet = (long)m_WorkingSetPC.NextValue()
                    };

                    break;
                }
                catch (InvalidOperationException e)
                {
                    if (retry)
                        throw e;
                    if (Environment.OSVersion.Platform == PlatformID.Unix 
                        || Environment.OSVersion.Platform == PlatformID.MacOSX)
                        throw e;
                    var newInstanceName = GetPerformanceCounterInstanceName(Process.GetCurrentProcess());
                    SetupPerformanceCounters(newInstanceName);
                    retry = true;
                }
            }

            var perfBuilder = new StringBuilder();

            perfBuilder.AppendLine("---------------------------------------------------");
            perfBuilder.AppendLine(string.Format("CPU Usage: {0}%, Physical Memory Usage: {1:N}, Total Thread Count: {2}", globalPerfData.CpuUsage.ToString("0.00"), globalPerfData.WorkingSet, globalPerfData.TotalThreadCount));
            perfBuilder.AppendLine(string.Format("AvailableWorkingThreads: {0}, AvailableCompletionPortThreads: {1}", globalPerfData.AvailableWorkingThreads, globalPerfData.AvailableCompletionPortThreads));
            perfBuilder.AppendLine(string.Format("MaxWorkingThreads: {0}, MaxCompletionPortThreads: {1}", globalPerfData.MaxWorkingThreads, globalPerfData.MaxCompletionPortThreads));

            Logger.LogDebug("Performance:" + perfBuilder.ToString());
        }