using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CpuProfileController
{
class Program
{
static void Main(string[] args)
{
MakeUsage(1);
}
static void MakeUsage(float level)
{
try
{
PerformanceCounter p = new PerformanceCounter("Processor", "% Processor Time", "_Total");
if (p == null)
{
return;
}
while (true)
{
if (p.NextValue() > level)
{
Thread.Sleep(10);
}
}
}
catch (Exception ex)
{
}
}
}
}
Microsoft .NET Framework 提供了PerformanceCounter这一对象,可以方便的得到当前机器的各种性能数据,包括CPU的使用率。
Thread.Sleep()方法主动让CPU进入休眠状态。