TimerViewModelBase.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Threading;
  6. using System.ComponentModel;
  7. using System.Threading.Tasks;
  8. using System.Reflection;
  9. using Aitex.Core.Util;
  10. using System.Windows;
  11. using Aitex.Core.RT.Log;
  12. namespace Aitex.Core.UI.MVVM
  13. {
  14. public abstract class TimerViewModelBase : ViewModelBase, ITimerViewModelBase
  15. {
  16. PeriodicJob _timer;
  17. //static List<TimerViewModelBase> _lstAll = new List<TimerViewModelBase>();
  18. protected bool _isStart = false;
  19. public static void StopAll()
  20. {
  21. //foreach (TimerViewModelBase vm in _lstAll)
  22. // vm.Stop();
  23. }
  24. public TimerViewModelBase(string name)
  25. {
  26. _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + name, false, true);
  27. //_lstAll.Add(this);
  28. }
  29. //
  30. protected virtual bool OnTimer()
  31. {
  32. try
  33. {
  34. Poll();
  35. }
  36. catch (Exception ex)
  37. {
  38. LOG.WriteExeption(ex);
  39. }
  40. return true;
  41. }
  42. public void Start()
  43. {
  44. _timer.Start();
  45. _isStart = true;
  46. }
  47. public void Stop()
  48. {
  49. _timer.Stop();
  50. _isStart = false;
  51. }
  52. public void Dispose()
  53. {
  54. Stop();
  55. }
  56. protected abstract void Poll();
  57. public virtual void UpdateData() { }
  58. public virtual void EnableTimer(bool enable)
  59. {
  60. if (enable)
  61. {
  62. _timer.Start();
  63. _isStart = true;
  64. }
  65. else
  66. {
  67. _timer.Pause();
  68. _isStart = false;
  69. }
  70. }
  71. }
  72. public interface ITimerViewModelBase
  73. {
  74. void EnableTimer(bool enable);
  75. }
  76. }