PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Diagnostics;
  4. using System.Windows.Threading;
  5. using Microsoft.Research.DynamicDataDisplay.Common;
  6. namespace PerfCounterChart
  7. {
  8. public class PerformanceInfo
  9. {
  10. public DateTime Time { get; set; }
  11. public double Value { get; set; }
  12. }
  13. public class PerformanceData : RingArray<PerformanceInfo>
  14. {
  15. private readonly PerformanceCounter counter;
  16. public PerformanceData(string categoryName, string counterName) : this(new PerformanceCounter(categoryName, counterName)) { }
  17. public PerformanceData(PerformanceCounter counter)
  18. : base(200)
  19. {
  20. if (counter == null)
  21. throw new ArgumentNullException("counter");
  22. this.counter = counter;
  23. timer.Tick += OnTimerTick;
  24. timer.Start();
  25. }
  26. private void OnTimerTick(object sender, EventArgs e)
  27. {
  28. var newInfo = new PerformanceInfo { Time = DateTime.Now, Value = counter.NextValue() };
  29. this.Add(newInfo);
  30. Debug.WriteLine(String.Format("{0}.{1}: {2}", newInfo.Time.Second, newInfo.Time.Millisecond, newInfo.Value));
  31. }
  32. private TimeSpan updateInterval = TimeSpan.FromMilliseconds(500);
  33. public TimeSpan UpdateInterval
  34. {
  35. get { return updateInterval; }
  36. set
  37. {
  38. updateInterval = value;
  39. timer.Interval = updateInterval;
  40. }
  41. }
  42. private readonly DispatcherTimer timer = new DispatcherTimer();
  43. public void Run()
  44. {
  45. timer.Interval = updateInterval;
  46. timer.IsEnabled = true;
  47. }
  48. public void Pause()
  49. {
  50. timer.IsEnabled = false;
  51. }
  52. }
  53. }