以图表的形式显示CPU使用情况

时间:2018-04-13 19:52:16

标签: c# visual-studio

PerformanceCounter CPUCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

chart1.Series["Usage %"].Points.AddY(CPUCounter);

返回错误消息:

  

System.ArgumentException:'系列数据点不支持值   键入System.Diagnostics.PerformanceCounter仅这些类型的值   可以使用:Double,Decimal,Single,int,long,uint,ulong,String,   DateTime,简称,ushort。'

我正在尝试在线图上显示CPU使用率数据以显示使用历史记录。

1 个答案:

答案 0 :(得分:2)

PerformanceCounter类仅用于引用特定的NT性能计数器组件。有了实例后,您可以拨打NextSample()CounterSample,或者只使用RawValue属性,如果尚未调用,则调用NextSample。

我希望这对你有用,给予或采取舍入/缩放:

chart1.Series["Usage %"].Points.AddY(CPUCounter.RawValue); // Use RawValue property here

RawValue返回一个long,你得到的异常消息表明长值是可以接受的。