性能计数器不显示

时间:2014-02-24 23:21:48

标签: c#

我有这个程序。我在运行此程序后尝试查看perfmon.exe中的计数器,但计数器在性能监视器中没有更改。

我使用最新的.NET运行Win 8.1,并以管理员身份运行Visual Studio。

class Program
{
    static void Main(string[] args)
    {
        if (CreatePerformanceCounters())
        {
            Console.WriteLine("Created performance counters");
            Console.WriteLine("Please restart application");
            Console.ReadKey();
            return;
        }
        var totalOperationsCounter = new PerformanceCounter("MyCategory", "# operations executed", "", false);
        var operationsPerSecondCounter = new PerformanceCounter("MyCategory", "# operations / sec", "", false);
        totalOperationsCounter.Increment();
        operationsPerSecondCounter.Increment();
    }
    private static bool CreatePerformanceCounters()
    {
        if (!PerformanceCounterCategory.Exists("MyCategory"))
        {
            CounterCreationDataCollection counters = new CounterCreationDataCollection 
            { 
                new CounterCreationData("# operations executed", "Total number of operations executed", PerformanceCounterType.NumberOfItems32), 
                new CounterCreationData("# operations / sec", "Number of operations executed per second", PerformanceCounterType.RateOfCountsPerSecond32)
            };
            PerformanceCounterCategory.Create("MyCategory", "Sample category for Codeproject", counters);
            return true;
        }
        return false;
    }
}

enter image description here

1 个答案:

答案 0 :(得分:4)

您的应用在写完计数器后立即退出。尝试让进程保持活动一段时间,你会在perfmon中看到计数器。