PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/Microsoft.Research.Visualization3D/MainLoops/TimeManager.cs

#
C# | 71 lines | 60 code | 11 blank | 0 comment | 2 complexity | d16747e0af7a615fff54749fc795c789 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Threading;
  6. using System.Diagnostics;
  7. namespace Microsoft.Research.Visualization3D.MainLoops
  8. {
  9. class TimeManager
  10. {
  11. private delegate void EmptyEventHandler();
  12. public delegate void TimedEventHandler(object sender, TimedEventArgs e);
  13. private long oldTime;
  14. private long delta;
  15. private float timeProportion = 100000.0f;
  16. Stopwatch stopwatch;
  17. private TimeEntity currentTimeEntity;
  18. public TimeEntity Current
  19. {
  20. get
  21. {
  22. long newTime = stopwatch.ElapsedTicks;
  23. delta = newTime - currentTimeEntity.TotalTime.Ticks;
  24. currentTimeEntity = new TimeEntity(TimeSpan.FromTicks(delta), TimeSpan.FromTicks(newTime));
  25. return currentTimeEntity;
  26. }
  27. }
  28. public event TimedEventHandler Update;
  29. public TimeManager()
  30. {
  31. currentTimeEntity = new TimeEntity(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(0));
  32. }
  33. private void RaiseUpdate(TimedEventArgs e)
  34. {
  35. if (Update != null)
  36. Update(this, e);
  37. }
  38. public void Start()
  39. {
  40. stopwatch = Stopwatch.StartNew();
  41. Dispatcher.CurrentDispatcher.BeginInvoke(new EmptyEventHandler(MainLoop), DispatcherPriority.ApplicationIdle, null);
  42. }
  43. private void MainLoop()
  44. {
  45. long newTime = stopwatch.ElapsedTicks;
  46. delta = newTime - currentTimeEntity.TotalTime.Ticks;
  47. currentTimeEntity = new TimeEntity(TimeSpan.FromTicks(delta), TimeSpan.FromTicks(newTime));
  48. RaiseUpdate(new TimedEventArgs { TimeEntity = currentTimeEntity });
  49. Dispatcher.CurrentDispatcher.BeginInvoke(new EmptyEventHandler(MainLoop), DispatcherPriority.ApplicationIdle, null);
  50. }
  51. }
  52. public class TimedEventArgs
  53. {
  54. public TimeEntity TimeEntity
  55. {
  56. get;
  57. set;
  58. }
  59. }
  60. }