C# 为什么我的PerformanceCounter在向方法传递计数器时总是返回0

C# 为什么我的PerformanceCounter在向方法传递计数器时总是返回0,c#,performancecounter,C#,Performancecounter,我已经发现,我的PerformanceCounter第一次出现是0,因此我必须多次调用它才能得到正确的结果 我让它工作得很好-看起来是这样的: public static float GetCpuUsage() { var cpuCounter = new PerformanceCounter(); cpuCounter.CategoryName = "Processor"; cpuCounter.CounterName = "% Processor Time";

我已经发现,我的PerformanceCounter第一次出现是0,因此我必须多次调用它才能得到正确的结果

我让它工作得很好-看起来是这样的:

public static float GetCpuUsage()
{
    var cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    // Prime it
    cpuCounter.NextValue();

    Thread.Sleep(500);

    return cpuCounter.NextValue();
}
public static float GetCpuUsage(PerformanceCounter counter)
{
    if (counter == null)
    {
        counter = new PerformanceCounter();
        counter.CategoryName = "Processor";
        counter.CounterName = "% Processor Time";
        counter.InstanceName = "_Total";
    }

    return counter.NextValue();
}
PerformanceCounter cpuCounter = null;
PerformanceCounter memoryCounter = null;

while (_isRunning)
{
    result.Cpu = GetCpuUsage(cpuCounter);
    result.Memory = GetAvailableMemory(memoryCounter);

    // Process result

    Thread.Sleep(500);
}
但是,我希望能够从多个PerformanceCounter获得结果,而不必为每个计数器等待半秒钟。我是这样做的:

public static float GetCpuUsage()
{
    var cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    // Prime it
    cpuCounter.NextValue();

    Thread.Sleep(500);

    return cpuCounter.NextValue();
}
public static float GetCpuUsage(PerformanceCounter counter)
{
    if (counter == null)
    {
        counter = new PerformanceCounter();
        counter.CategoryName = "Processor";
        counter.CounterName = "% Processor Time";
        counter.InstanceName = "_Total";
    }

    return counter.NextValue();
}
PerformanceCounter cpuCounter = null;
PerformanceCounter memoryCounter = null;

while (_isRunning)
{
    result.Cpu = GetCpuUsage(cpuCounter);
    result.Memory = GetAvailableMemory(memoryCounter);

    // Process result

    Thread.Sleep(500);
}
我这样称呼它:

public static float GetCpuUsage()
{
    var cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    // Prime it
    cpuCounter.NextValue();

    Thread.Sleep(500);

    return cpuCounter.NextValue();
}
public static float GetCpuUsage(PerformanceCounter counter)
{
    if (counter == null)
    {
        counter = new PerformanceCounter();
        counter.CategoryName = "Processor";
        counter.CounterName = "% Processor Time";
        counter.InstanceName = "_Total";
    }

    return counter.NextValue();
}
PerformanceCounter cpuCounter = null;
PerformanceCounter memoryCounter = null;

while (_isRunning)
{
    result.Cpu = GetCpuUsage(cpuCounter);
    result.Memory = GetAvailableMemory(memoryCounter);

    // Process result

    Thread.Sleep(500);
}
我认为这是一个非常好的主意——每次都将相同的反实例传递给该方法

然而,它实际上并不工作,cpu总是0

它告诉我关于PerformanceCounters有一些基本的东西我不了解(我链接的问题中没有提到)


是什么让PerformanceCounter工作的?因为在每次下一个值调用之间等待500毫秒似乎不是唯一的区别。

我找到了原因-这是一个基本的.NET错误,与PerformanceCounters无关。如果我将null对象(在本例中为PerformanceCounter)传递给方法并将其设置为对象的实例,则原始null引用不会更新为指向新对象。这意味着PerformanceCounter将继续为空

解决方案是在调用方法之前实例化PerformanceCounter

不幸的是,这个解决方案与所问问题的核心主题并不相关,但我在提问时并不知道这一点