0

I want to use Dynamic Data Display library to display the CPU performance with WPF.

Here is the code.

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;

namespace WpfPerformance
{
    public partial class MainWindow : Window
    {
        private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();
        private PerformanceCounter cpuPerformance = new PerformanceCounter();
        private DispatcherTimer timer = new DispatcherTimer();
        private int i = 0;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void AnimatedPlot(object sender, EventArgs e)
        {
            cpuPerformance.CategoryName = "Processor";
            cpuPerformance.CounterName = "% Processor Time";
            cpuPerformance.InstanceName = "_Total";

            double x = i;
            double y = cpuPerformance.NextValue();

            Point point = new Point(x, y);
            dataSource.AppendAsync(base.Dispatcher, point);

            cpuUsageText.Text = String.Format("{0:0}%", y);
            i++;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            plotter.AddLineGraph(dataSource, Colors.Green, 2, "Percentage");
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(AnimatedPlot);
            timer.IsEnabled = true;
            plotter.Viewport.FitToView();
        }
    }
}

The result likes the image below. CPU

However the code is for one cpu only. For the modern machine, the machine has many cores. What I want is to display the cores performance.

So my idea is to use tasks to handle cores.

private PerformanceCounter[] cpuPerformance = new PerformanceCounter[System.Environment.ProcessorCount];

For each core, I want to use a task to do the performance show. The updated code is

public partial class MainWindow : Window
{
    //private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();
    private PerformanceCounter[] cpuPerformance = new PerformanceCounter[System.Environment.ProcessorCount];
    private DispatcherTimer timer = new DispatcherTimer();
    private int i = 0;

    public MainWindow()
    {
        InitializeComponent();
    }
    private async void AnimatedPlot(object sender, EventArgs e)
    {
        var t = new Task[cpuPerformance.Length];
        for (int j = 0; j < cpuPerformance.Length; j++)
        {
            t[j] = new Task(() =>
            {
                ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();
                plotter.AddLineGraph(dataSource, Colors.Green, j+1, "Percentage");
                cpuPerformance[j] = new PerformanceCounter("Processor", "% Processor Time", j.ToString());
                double x = i;
                double y = cpuPerformance[j].NextValue();

                Point point = new Point(x, y);
                dataSource.AppendAsync(base.Dispatcher, point);

                cpuUsageText.Text = String.Format("{0:0}%", y);
                i++;
            }
            );
        }
        await Task.WhenAll(t);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += new EventHandler(AnimatedPlot);
        timer.IsEnabled = true;
        plotter.Viewport.FitToView();
    }
}

But I get nothing to display. Help me to figure out what is wrong.

Bigeyes
  • 1,508
  • 2
  • 23
  • 42

1 Answers1

1

a couple of things:

In your "single core" example you're instantiating PerformanceCounter once. In your multi core you're doing it every time you want to update the graph.

Then again, I wonder if you're using it correctly. It almost seems like you're assuming that these tasks would run on the different cores and that's what makes them count that cores performance.

I'm not accustomed to that library but I'd do this:

  1. get rid of the tasks.

  2. instantiate the PerformanceCounters once in the constructor

  3. read up on how to use the PerformanceCounter class correctly.

Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
  • I don't know what you mean with that comment. – Markus Hütter Jun 22 '16 at 21:58
  • If you look at [this answer](http://stackoverflow.com/questions/5537286/how-to-get-cpu-usage-for-more-than-2-cores), in that answer each processor uses its own performanceCounter. What did you mean the constructor? Did you mean I need to pass performanceCounters into MainWindow's constructor? – Bigeyes Jun 22 '16 at 22:11
  • well, is it your intention to create a couple of new PerformanceCounters every second, or do you want to create a couple of new PerformanceCounters at the start of your program ( like in `public MainWindow()` ) and just _read_ the values of the PerformanceCounters every second? – Markus Hütter Jun 22 '16 at 22:28
  • You are right, Now I have `PerformanceCounters` in the constructor. Then how to pass each core's performance to the event method if without for loop? – Bigeyes Jun 23 '16 at 01:02
  • I never said without for loop. I said without Task. – Markus Hütter Jun 23 '16 at 01:10
  • I hope a little code assistance as I still can't figure it out. The project is on [OneDrive CPU-Performance](https://onedrive.live.com/?id=3A8BB9EAEEFF1DB%214159&cid=03A8BB9EAEEFF1DB). – Bigeyes Jun 23 '16 at 11:44
  • I'm sorry but no. This is programming 101. You'll figure it out. Most of what you do in the `AnimatedPlot()` should not be in there. `AnimatedPlot()` should only contain a for loop that cycles through the `PerformanceCounter[]` and add the `NextValue()` to the corresponding `ObservableDataSource`. I don't think you understand your own code and I'm sorry, I'm not the one to educate you on that. Figure it out! Debug your code! Step through it! These are basics! You can do it! – Markus Hütter Jun 23 '16 at 12:43
  • @Bigeyes so, how is your progress? – Markus Hütter Jun 24 '16 at 23:49
  • It just plots a straight line(No curve). My machine has 4 cores, – Bigeyes Jun 24 '16 at 23:51