在一定时间内保持低CPU使用率。c#
我有下面的代码,它只允许我的应用程序在CPU使用率低于一定时间后打开。但我只是需要一些帮助来添加一些东西,确保使用率保持在这个低水平至少5秒,这样我就可以避免CPU使用率下降在一定时间内保持低CPU使用率。c#,c#,cpu-usage,performancecounter,C#,Cpu Usage,Performancecounter,我有下面的代码,它只允许我的应用程序在CPU使用率低于一定时间后打开。但我只是需要一些帮助来添加一些东西,确保使用率保持在这个低水平至少5秒,这样我就可以避免CPU使用率下降 cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var usage = cpuUsage.NextValue(); do { Thread.Sleep(TimeSpan.FromSeconds(1));
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();
int secondsWhileLowUsage=0;
做{
cpuUsage=新性能计数器(“处理器”、“处理器时间百分比”、“总计”);
var usage=cpuUsage.NextValue();
做
{
线程睡眠(TimeSpan.FromSeconds(1));
用法=cpuUsage.NextValue();
如果(使用量>10.00)
第二次时的平均值=0;
Console.WriteLine(用法+“%”);
}而(使用量>10.00);
第二,当语言为++时;
}while(第二次当wusage<5时)
Process proc=新流程();
proc.StartInfo=newprocessstartinfo(@“C:\Documents and Settings\rcgames\Desktop\Game1.exe”);
proc.Start();
您是否尝试过实现这一目标?没有。。。我想添加一个睡眠的for循环?我还不是百分之百确定。这就是为什么我要征求建议@vensonNo我的意思是,那代码的目的是什么?你想做什么,为什么要这样做?如果在5分钟内保持在10%以下,那么你开始游戏,然后在游戏结束后,CPU峰值出现?在这一点上,您对这个特定的代码没有任何收获。如果游戏是CPU密集型的,我会反对在不能处理它的机器上运行它。我同意@Venson的观点,你到底想用这个来完成什么,我认为还需要更多的细节。
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();