Question

I have the following code which only allows my application to open after the CPU Usage is below a certain amount of time. But I just need some help adding something that makes sure the usage stays this low for at least 5 seconds so I can avoid any down spikes in CPU Usage.

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();
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top