使用WPF动态显示CPU使用率

2023/7/6 02:13:34

"使用WPF动态显示CPU使用率

基于WPF的开源图表控件有很多,大多数都是静态图表,如果需要绘制CPU使用率这样的动态数据就显得力不从心,微软开源的DynamicDataDisplay控件弥补了这个不足,为了做个备忘,我用它来实时绘制CPU使用率曲线,当然,这个控件也可以绘制动态线图、气泡图和热力图,具体可参阅官网示例代码,使用方法非常简单,下面就直接贴代码,文本末尾提供VS2013的示例代码下载。

1、首先需要在项目中引用DynamicDataDisplay.dll,该程序集可通过官网下载或者NuGet方式搜索获去,在包控制台执行以下命令即可自动引用。

<span style=color: black;>PM> Install-Package DynamicDataDisplay

2、在XAML文件中引用D3命名空间,同时添加一个名为ChartPlotter的图表控件。

<span style=color: blue;><<span style=color: #a31515;>Window <span style=color: red;>x<span style=color: blue;>:<span style=color: red;>Class<span style=color: blue;>=WpfCpuUsageSample.MainWindow <span style=color: red;>xmlns<span style=color: blue;>=http://schemas.microsoft.com/winfx/2006/xaml/presentation <span style=color: red;>xmlns<span style=color: blue;>:<span style=color: red;>x<span style=color: blue;>=http://schemas.microsoft.com/winfx/2006/xaml <span style=color: red;>xmlns<span style=color: blue;>:<span style=color: red;>d3<span style=color: blue;>=http://research.microsoft.com/DynamicDataDisplay/1.0 <span style=color: red;>Title<span style=color: blue;>=MainWindow <span style=color: red;>Loaded<span style=color: blue;>=Window_Loaded <span style=color: red;>Height<span style=color: blue;>=350 <span style=color: red;>Width<span style=color: blue;>=525> <<span style=color: #a31515;>Grid<span style=color: blue;>> <<span style=color: #a31515;>d3<span style=color: blue;>:<span style=color: #a31515;>ChartPlotter <span style=color: red;>Grid.Column<span style=color: blue;>=0 <span style=color: red;>Grid.Row<span style=color: blue;>=0 <span style=color: red;>Name<span style=color: blue;>=plotter/> </<span style=color: #a31515;>Grid<span style=color: blue;>> </<span style=color: #a31515;>Window<span style=color: blue;>>

3、在后台窗体类中申明数据源ObservableDataSource用于为图表控件提供数据,PerformanceCounter是性能计数器,可通过它来获取CPU使用率,DispatcherTimer是一个基于WPF的计时器,支持在子线程异步通知UI线程,在窗口加载事件中初始化这些类,并未计时器绑定一个处理程序,用于获取CPU使用率,并将当前的使用率加入到动态曲线中。

<span style=color: blue;>public partial class <span style=color: #2b91af;>MainWindow <span style=color: black;>: <span style=color: #2b91af;>Window <span style=color: black;>{ <span style=color: blue;>private <span style=color: #2b91af;>ObservableDataSource<span style=color: black;><<span style=color: #2b91af;>Point<span style=color: black;>> dataSource = <span style=color: blue;>new <span style=color: #2b91af;>ObservableDataSource<span style=color: black;><<span style=color: #2b91af;>Point<span style=color: black;>>(); <span style=color: blue;>private <span style=color: #2b91af;>PerformanceCounter <span style=color: black;>performanceCounter = <span style=color: blue;>new <span style=color: #2b91af;>PerformanceCounter<span style=color: black;>(); <span style=color: blue;>private <span style=color: #2b91af;>DispatcherTimer <span style=color: black;>dispatcherTimer = <span style=color: blue;>new <span style=color: #2b91af;>DispatcherTimer<span style=color: black;>(); <span style=color: blue;>private int <span style=color: black;>currentSecond = 0;

</span><span style=""color: blue;"">public </span><span style=""color: black;"">MainWindow()
{
    InitializeComponent();
}

</span><span style=""color: blue;"">private void </span><span style=""color: black;"">Window_Loaded(</span><span style=""color: blue;"">object </span><span style=""color: black;"">sender, </span><span style=""color: #2b91af;"">RoutedEventArgs </span><span style=""color: black;"">e)
{
    plotter.AddLineGraph(dataSource, </span><span style=""color: #2b91af;"">Colors</span><span style=""color: black;"">.Green, 2, </span><span style=""color: #a31515;"">""Percentage""</span><span style=""color: black;"">);
    plotter.LegendVisible = </span><span style=""color: blue;"">false</span><span style=""color: black;"">;
    dispatcherTimer.Interval = </span><span style=""color: #2b91af;"">TimeSpan</span><span style=""color: black;"">.FromSeconds(1);
    dispatcherTimer.Tick += timer_Tick;
    dispatcherTimer.IsEnabled = </span><span style=""color: blue;"">true</span><span style=""color: black;"">;
    plotter.Viewport.FitToView();
}

</span><span style=""color: blue;"">private void </span><span style=""color: black;"">timer_Tick(</span><span style=""color: blue;"">object </span><span style=""color: black;"">sender, </span><span style=""color: #2b91af;"">EventArgs </span><span style=""color: black;"">e)
{
    performanceCounter.CategoryName = </span><span style=""color: #a31515;"">""Processor""</span><span style=""color: black;"">;
    performanceCounter.CounterName = </span><span style=""color: #a31515;"">""% Processor Time""</span><span style=""color: black;"">;
    performanceCounter.InstanceName = </span><span style=""color: #a31515;"">""_Total""</span><span style=""color: black;"">;

    </span><span style=""color: blue;"">double </span><span style=""color: black;"">x = currentSecond;
    </span><span style=""color: blue;"">double </span><span style=""color: black;"">y = performanceCounter.NextValue();

    </span><span style=""color: #2b91af;"">Point </span><span style=""color: black;"">point = </span><span style=""color: blue;"">new </span><span style=""color: #2b91af;"">Point</span><span style=""color: black;"">(x, y);
    dataSource.AppendAsync(</span><span style=""color: blue;"">base</span><span style=""color: black;"">.Dispatcher, point);

    currentSecond++;
}

}

[零度下载](https://oss.xcode.me/uploads/2015/07/WpfCpuUsage.zip)"