Using WPF to dynamically display CPU usage

Source: Internet
Author: User

Using WPF to dynamically display CPU usageOpen Source Framework Admin 9 month ago (07-14) 1337 views

WPF-based open-source chart controls have a lot, most of them are static charts, if you need to draw the CPU utilization of such dynamic data is inadequate, Microsoft Open source Dynamicdatadisplay control to compensate for this deficiency, in order to make a memo, I use it to draw the CPU usage curve in real time, Of course, this control can also draw dynamic line diagram, bubble chart and thermal force diagram, in particular, see the official Website sample code, the use of the method is very simple, the following direct code, the end of the text provides VS2013 sample code download.

1, first need to reference DynamicDataDisplay.dll in the project, the Assembly can be downloaded through the official website or NuGet search, the package console execute the following command can be automatically referenced.

Pm> Install-package Dynamicdatadisplay

2. Refer to the D3 namespace in the XAML file and add a chart control named Chartplotter.

<window x:class= "Wpfcpuusagesample.mainwindow"        xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "        xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "        xmlns:d3="/http/ research.microsoft.com/dynamicdatadisplay/1.0 "        title=" MainWindow "loaded=" window_loaded "height=" "Width=" 525 ">    <Grid>        <d3:chartplotter grid.column=" 0 "grid.row=" 0 "  name=" plotter "/>    </ Grid></window>

3. In the Background form class, declare that the data source Observabledatasource is used to provide data for the chart control, PerformanceCounter is a performance counter, which can be used to obtain CPU utilization, DispatcherTimer is a WPF-based timer that supports asynchronous notification of UI threads on a child thread, initializes these classes in a window load event, does not have a timer bound to a handler, gets CPU usage, and adds the current usage to the dynamic curve.

Public partial class mainwindow:window{private observabledatasource<point> DataSource = new Observabledatasour    Ce<point> ();    Private PerformanceCounter PerformanceCounter = new PerformanceCounter ();    Private DispatcherTimer DispatcherTimer = new DispatcherTimer ();    private int currentsecond = 0;    Public MainWindow () {InitializeComponent (); The private void window_loaded (object sender, RoutedEventArgs e) {plotter.        Addlinegraph (DataSource, Colors.green, 2, "Percentage"); Plotter.        Legendvisible = false;        Dispatchertimer.interval = Timespan.fromseconds (1);        Dispatchertimer.tick + = Timer_tick;        Dispatchertimer.isenabled = true; Plotter.    Viewport.fittoview ();        } private void Timer_tick (object sender, EventArgs e) {performancecounter.categoryname = "Processor";        Performancecounter.countername = "% Processor time";        Performancecounter.instancename = "_total"; Double x = currentsecOnd        Double y = performancecounter.nextvalue ();        Point point = new Point (x, y); Datasource.appendasync (base.        Dispatcher, point);    currentsecond++; }}

Using WPF to dynamically display CPU usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.