-1

我有以下代码,它只允许我的应用程序在 CPU 使用率低于一定时间后打开。但是我只需要一些帮助来添加一些东西,以确保使用率保持在这个低水平至少 5 秒,这样我就可以避免 CPU 使用率的任何下降峰值。

cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");
var usage = cpuUsage.NextValue();
do
{
    Thread.Sleep(TimeSpan.FromSeconds(1));
    usage = cpuUsage.NextValue();
    Console.WriteLine(usage + "%");
} while (usage > 10.00);

Process proc = new Process();
proc.StartInfo = new ProcessStartInfo(@"C:\Documents and Settings\rcgames\Desktop\Game1.exe");
proc.Start();
4

1 回答 1

1
int secondsWhileLowUsage = 0;     
do {
    cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    var usage = cpuUsage.NextValue();
    do
    {
        Thread.Sleep(TimeSpan.FromSeconds(1));
        usage = cpuUsage.NextValue();
        if (usage > 10.00)
            secondsWhileLowUsage = 0;

        Console.WriteLine(usage + "%");
    } while (usage > 10.00);
    secondsWhileLowUsage ++; 
} while (secondsWhileLowUsage < 5)

Process proc = new Process();
proc.StartInfo = new ProcessStartInfo(@"C:\Documents and Settings\rcgames\Desktop\Game1.exe");
proc.Start();
于 2013-08-05T15:31:44.037 回答