利用Process获取CPU占用率

using (var pro = Process)
{
    //间隔时间(毫秒)
    int interval = 1000;
    //上次记录的CPU时间
    var prevCpuTime = TimeSpan.Zero;
    while (true)
    {
        try
        {
            //当前时间
            var curTime = pro.TotalProcessorTime;
            //多核占用
            //var value = (curTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
            //单核占用
            var value = (curTime - prevCpuTime).TotalMilliseconds / interval * 100;
            prevCpuTime = curTime;
            CPU = value;
        }catch{
            CPU = 0;
        }
        Thread.Sleep(interval);
    }
}