/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
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Windows.Threading; 6using System.Diagnostics; 7 8namespace Microsoft.Research.Visualization3D.MainLoops 9{ 10 class TimeManager 11 { 12 private delegate void EmptyEventHandler(); 13 public delegate void TimedEventHandler(object sender, TimedEventArgs e); 14 private long oldTime; 15 private long delta; 16 private float timeProportion = 100000.0f; 17 Stopwatch stopwatch; 18 19 private TimeEntity currentTimeEntity; 20 21 public TimeEntity Current 22 { 23 get 24 { 25 long newTime = stopwatch.ElapsedTicks; 26 delta = newTime - currentTimeEntity.TotalTime.Ticks; 27 currentTimeEntity = new TimeEntity(TimeSpan.FromTicks(delta), TimeSpan.FromTicks(newTime)); 28 return currentTimeEntity; 29 } 30 } 31 32 33 34 public event TimedEventHandler Update; 35 36 public TimeManager() 37 { 38 currentTimeEntity = new TimeEntity(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(0)); 39 } 40 41 private void RaiseUpdate(TimedEventArgs e) 42 { 43 if (Update != null) 44 Update(this, e); 45 } 46 47 public void Start() 48 { 49 stopwatch = Stopwatch.StartNew(); 50 Dispatcher.CurrentDispatcher.BeginInvoke(new EmptyEventHandler(MainLoop), DispatcherPriority.ApplicationIdle, null); 51 } 52 53 private void MainLoop() 54 { 55 long newTime = stopwatch.ElapsedTicks; 56 delta = newTime - currentTimeEntity.TotalTime.Ticks; 57 currentTimeEntity = new TimeEntity(TimeSpan.FromTicks(delta), TimeSpan.FromTicks(newTime)); 58 RaiseUpdate(new TimedEventArgs { TimeEntity = currentTimeEntity }); 59 Dispatcher.CurrentDispatcher.BeginInvoke(new EmptyEventHandler(MainLoop), DispatcherPriority.ApplicationIdle, null); 60 } 61 } 62 63 public class TimedEventArgs 64 { 65 public TimeEntity TimeEntity 66 { 67 get; 68 set; 69 } 70 } 71}