Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

Monitoring Process Statistics in C# WPF

Rate me:
Please Sign up or sign in to vote.
4.85/5 (24 votes)
25 Jul 2009CPOL3 min read 127.4K   7.8K   79  
In this article, I will explain the performance monitoring of any instance in the Form of statistics and graphs as well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Collections.Specialized;

using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System.Collections.ObjectModel;
using Microsoft.Research.DynamicDataDisplay.Charts;

namespace MemoryPerformanceMonitoring
{
	/// <summary>
	/// Interaction logic for Window2.xaml
	/// </summary>
	public partial class Window2 : Window
	{
        string strProcName = null;
        string strCategoryName = null;
        string strCounterName = null;
        string strDisplayName = null;

        public Window2(string sCategoryName, string sCounterName, string sProcName, string sDisplayName)
		{
			InitializeComponent();
            
            strCategoryName = sCategoryName;
            strCounterName = sCounterName;
            strProcName = sProcName;
            strDisplayName = sDisplayName;

			Loaded += new RoutedEventHandler(Window2_Loaded);
		}

		private void Window2_Loaded(object sender, RoutedEventArgs e)
		{

            CreatePerformanceGraph(strCategoryName, strCounterName, strProcName);
            
		}

		private LineGraph CreatePerformanceGraph(string categoryName, string counterName, string instanceName)
		{
			PerformanceData data = new PerformanceData(new PerformanceCounter(categoryName, counterName, instanceName));

			var filteredData = new FilteringDataSource<PerformanceInfo>(data, new MaxSizeFilter());

			var ds = new EnumerableDataSource<PerformanceInfo>(filteredData);
			ds.SetXMapping(pi => pi.Time.TimeOfDay.TotalSeconds);

            Color cr = new Color();
            LineGraph chart = plotter.AddLineGraph(ds, 3.0, String.Format("{0} - {1}", instanceName, strDisplayName));
                   
            if(strDisplayName.Contains("Memory"))
            {
                ds.SetYMapping(pi => pi.Value/1024);
                plotter.AddLineGraph(ds, cr, 3.0, String.Format("{0} - {1}", "X axis = Total# of seconds from today’s start", "Y axis = K Bytes"));
            }
            else
            {
                ds.SetYMapping(pi => pi.Value);
                plotter.AddLineGraph(ds, cr, 3.0, String.Format("{0} - {1}", "X axis = Total# of seconds from today’s start", "Y axis = CPU Usage"));
            }
         
             return chart;
		}

        private void chart_DataChanged(object sender, EventArgs e)
        {
            LineGraph graph = (LineGraph)sender;

            double mbytes = graph.DataSource.GetPoints().LastOrDefault().Y;

            graph.Description = new PenDescription(String.Format("Memory - available {0} MBytes", mbytes));
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions