Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的CPU使用率一直是0%?_C#_.net_Winforms - Fatal编程技术网

C# 为什么我的CPU使用率一直是0%?

C# 为什么我的CPU使用率一直是0%?,c#,.net,winforms,C#,.net,Winforms,这就是我在表格1中所做的: void PopulateApplications() { DoubleBufferedd(dataGridView1, true); int rcount = dataGridView1.Rows.Count; int rcurIndex = 0; foreach (Process p in Process.GetProcesses()) { try { if (Fi

这就是我在表格1中所做的:

void PopulateApplications()
{
    DoubleBufferedd(dataGridView1, true);

    int rcount = dataGridView1.Rows.Count;
    int rcurIndex = 0;

    foreach (Process p in Process.GetProcesses())
    {
        try
        {
            if (File.Exists(p.MainModule.FileName))
            {
                memoryUsage = Core.getallmemoryusage(p.ProcessName);
                Core.getcpu(p.ProcessName);
                cpuusage = Core.processes;

                var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                Image ima = icon.ToBitmap();
                ima = resizeImage(ima, new Size(25, 25));
                ima = (Image)(new Bitmap(ima, new Size(25, 25)));
                String status = p.Responding ? "Running" : "Not Responding";

                if (rcurIndex < rcount - 1)
                {
                    var currentRow = dataGridView1.Rows[rcurIndex];
                    currentRow.Cells[0].Value = ima;
                    currentRow.Cells[1].Value = p.ProcessName;
                    currentRow.Cells[2].Value = cpuusage;
                    currentRow.Cells[3].Value = memoryUsage;
                    currentRow.Cells[4].Value = status;
                }
                else
                {
                    dataGridView1.Rows.Add(ima, p.ProcessName,cpuusage,memoryUsage, status);//false, ima, p.ProcessName, status);
                }

                rcurIndex++;
            }
        }
        catch ( Exception e)
        {
            string t = "error";
        }
    }

    if (rcurIndex < rcount - 1)
    {
        for (int i = rcurIndex; i < rcount - 1; i++)
        {
            dataGridView1.Rows.RemoveAt(rcurIndex);
        }
    }
}
问题在于
getcpu
方法。我需要让它每隔1000毫秒休眠几次,以获得CPU使用率。如果我在这个方法上使用断点,我最终会得到这个值。问题是,当我每5秒调用form1中的方法时,它也会每5秒调用并执行
getcpu
,而这些线程睡眠会使它工作得非常慢。如果我将线程睡眠设置为10ms,速度会更快,但大多数进程的使用率为0%或100%

public static string  getcpu(string name)
{
    var cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");

    processes = Convert.ToInt32(cpuload.NextValue()) + "%";
    System.Threading.Thread.Sleep(1000);

    processes = cpuload.NextValue() + "%";
    System.Threading.Thread.Sleep(1000);

    processes = cpuload.NextValue() + "%";
    System.Threading.Thread.Sleep(1000);

    processes = cpuload.NextValue() + "%";
    System.Threading.Thread.Sleep(1000);

    processes = cpuload.NextValue() + "%";
    System.Threading.Thread.Sleep(1000);

    return processes;
}

测量
%处理器时间时
,需要
线程.Sleep(1000)
,因为
.NextValue()
调用确定时间,处理器是从上次调用
.NextValue()
以来使用的。有关此计算的更多信息,请查看

有几点建议:

1) 由于您传递的是一个进程的名称,因此我假设您想要测量单个进程的处理器时间。但是,由于您没有在方法中使用该参数,因此您要测量的是总体平均系统处理器时间

因此,如果您想测量单个流程的性能,可以使用以下方法:

public static double GetCpuUsage(Process process)
{
    PerformanceCounter cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Process";
    cpuCounter.InstanceName = process.ProcessName;
    cpuCounter.CounterName = "% Processor Time";

    // The first call will always return 0
    cpuCounter.NextValue();
    // That's why we need to sleep 1 second 
    Thread.Sleep(1000);
    // The second call determines, the % of time that the monitored process uses on 
    // % User time for a single processor. 
    // So the limit is 100% * the number of processors you have.
    double processorUsagePercent = cpuCounter.NextValue();

    // Hence we need to divide by the number of processors to get the average CPU usage of one process during the time measured
    return processorUsagePercent / Environment.ProcessorCount;
}
请注意计数器类别的
“处理器”
“处理”
之间的区别

2) 为什么连续多次调用
.NextValue()
,然后只返回最后一个值?这使得你的方法很慢。上面示例中显示的两个计算就足够了

3) 要监视多个进程时,不必在每次调用
.NextValue()
之间等待一秒钟。您只需确保自上次在特定计数器上调用
.NextValue()
以来至少已经过了一秒钟。因此,对于多个流程,您可以使用如下方法:

public static Dictionary<Process, double> GetCpuUsages(Process[] processes)
{
    // One performance counter is required per process
    PerformanceCounter[] counters = new PerformanceCounter[processes.Length];

    // Instantiate a new counter per process
    for(int i = 0; i < processes.Length; i++)
    {
        PerformanceCounter processorTimeCounter = new PerformanceCounter(
            "Process",
            "% Processor Time",
            processes[i].ProcessName);
        // Call NextValue once to have a reference value
        processorTimeCounter.NextValue();
        // Add it to the array
        counters[i] = processorTimeCounter;
    }

    // Sleep one second to have accurate measurement
    Thread.Sleep(1000); 
    // Here we store the processes along with their measurement in a dictionary
    Dictionary<Process, double> cpuUsageDictionary = new Dictionary<Process, double>();

    for (int i = 0; i < counters.Length; i++)
    {
        // Determine CPU usage and divide by the number of cores
        double cpuUsage = counters[i].NextValue() / Environment.ProcessorCount;
        // And now we add one key/value pair per process along with its cpu time measurement
        cpuUsageDictionary.Add(processes[i],  cpuUsage);
    }

    return cpuUsageDictionary;
}
公共静态字典getcpuusage(进程[]进程)
{
//每个进程需要一个性能计数器
PerformanceCounter[]计数器=新的PerformanceCounter[processs.Length];
//为每个进程实例化一个新计数器
for(int i=0;i
Rotem right我用name变量更新了getcpu方法,改变了我的问题,并且我没有在所有进程上重复,只在form1中重复一次。但我在每个过程中都得到了0%。例如,在测试列表中,我看到1个索引和:chorme CPU0%,每个进程都是一样的。但在form1中,它有时会在某些进程上抛出异常访问被拒绝,但这些是系统进程,所以我不介意它们被拒绝访问。我只是再次更新了我的问题,显示内存使用部分工作正常,没有问题。cpu使用仍然不起作用。在问题的底部用我现在尝试的内容再次更新了我的问题。尝试了另一种解决方案,但也不起作用。尝试了这个解决方案:但是我如何使用getcpu方法中的进程名以及现在在哪里?(变量名)除了需要将结果除以处理器的数量(即System.Environment.ProcessorCount)之外,我看不出您的代码有任何错误,它对我来说是有效的。如果尝试获取%空闲时间(即传入“idle”作为第三个参数,而不是处理器名称或“\u Total”),会发生什么情况
public static Dictionary<Process, double> GetCpuUsages(Process[] processes)
{
    // One performance counter is required per process
    PerformanceCounter[] counters = new PerformanceCounter[processes.Length];

    // Instantiate a new counter per process
    for(int i = 0; i < processes.Length; i++)
    {
        PerformanceCounter processorTimeCounter = new PerformanceCounter(
            "Process",
            "% Processor Time",
            processes[i].ProcessName);
        // Call NextValue once to have a reference value
        processorTimeCounter.NextValue();
        // Add it to the array
        counters[i] = processorTimeCounter;
    }

    // Sleep one second to have accurate measurement
    Thread.Sleep(1000); 
    // Here we store the processes along with their measurement in a dictionary
    Dictionary<Process, double> cpuUsageDictionary = new Dictionary<Process, double>();

    for (int i = 0; i < counters.Length; i++)
    {
        // Determine CPU usage and divide by the number of cores
        double cpuUsage = counters[i].NextValue() / Environment.ProcessorCount;
        // And now we add one key/value pair per process along with its cpu time measurement
        cpuUsageDictionary.Add(processes[i],  cpuUsage);
    }

    return cpuUsageDictionary;
}