Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Representing my constantly updating data on a C# WPF chart

$
0
0

I have worked out a simple set of codes that shows my current CPU Usage that will be updated every second. 

I want to represent this information on a chart in WPF. I am trying to use WPF Toolkit to do this. I am looking to create a line chart similar to Windows Task Manager(performance tab). 

Can someone guide me how to represent my CPU usage information on a chart please. I am unable to find any examples/ documentations that could guide me to do this. Thanks for help. 

Part of the codes obtaining my CPU Usage every seconds is as follows:

class Monitor
{
    Stats stats = new Stats();

    public delegate void CPUEventHandler(object sender, CPUEventArgs args);
    public event CPUEventHandler CPUEvent;

    DispatcherTimer timer = new DispatcherTimer();

    private static double cpuCurrent;

    public Monitor()
    {
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += TimeChanged;
        timer.Start();
    }

    void TriggerCPUEvent(CPUEventArgs args)
    {
        if (CPUEvent != null)
        {
            CPUEvent(this, args);
        }
    }

    public void TimeChanged(object sender, EventArgs e)
    {
        cpuCurrent = stats.GetCurrentCpuUsage();

        TriggerCPUEvent(new CPUEventArgs(cpuCurrent));
    }
}

class Stats
{
    private PerformanceCounter cpuCounter;

    public Stats()
    {
        cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    }

    public float GetCurrentCpuUsage()
    {
        return cpuCounter.NextValue();
    }
}




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>