PageRenderTime 52ms CodeModel.GetById 46ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/Samples/v0.2/PerfCounter/PerformanceDataSource.cs

#
C# | 63 lines | 53 code | 10 blank | 0 comment | 2 complexity | 9f578f5f7aef6313fc2d26ecb7111a62 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
 1using System;
 2using System.Collections.ObjectModel;
 3using System.Diagnostics;
 4using System.Windows.Threading;
 5using Microsoft.Research.DynamicDataDisplay.Common;
 6
 7namespace PerfCounterChart
 8{
 9	public class PerformanceInfo
10	{
11		public DateTime Time { get; set; }
12		public double Value { get; set; }
13	}
14
15	public class PerformanceData : RingArray<PerformanceInfo>
16	{
17		private readonly PerformanceCounter counter;
18		public PerformanceData(string categoryName, string counterName) : this(new PerformanceCounter(categoryName, counterName)) { }
19
20		public PerformanceData(PerformanceCounter counter)
21			: base(200)
22		{
23			if (counter == null)
24				throw new ArgumentNullException("counter");
25
26			this.counter = counter;
27			timer.Tick += OnTimerTick;
28			timer.Start();
29		}
30
31		private void OnTimerTick(object sender, EventArgs e)
32		{
33			var newInfo = new PerformanceInfo { Time = DateTime.Now, Value = counter.NextValue() };
34			this.Add(newInfo);
35
36			Debug.WriteLine(String.Format("{0}.{1}: {2}", newInfo.Time.Second, newInfo.Time.Millisecond, newInfo.Value));
37		}
38
39		private TimeSpan updateInterval = TimeSpan.FromMilliseconds(500);
40		public TimeSpan UpdateInterval
41		{
42			get { return updateInterval; }
43			set
44			{
45				updateInterval = value;
46				timer.Interval = updateInterval;
47			}
48		}
49
50		private readonly DispatcherTimer timer = new DispatcherTimer();
51
52		public void Run()
53		{
54			timer.Interval = updateInterval;
55			timer.IsEnabled = true;
56		}
57
58		public void Pause()
59		{
60			timer.IsEnabled = false;
61		}
62	}
63}