PeriodicJob.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.Util
  8. {
  9. public class PeriodicJob
  10. {
  11. Thread _thread;
  12. int _interval;
  13. DeviceTimer _elapseTimer;
  14. Func<bool> _func;
  15. CancellationTokenSource _cancelFlag = new CancellationTokenSource(); //for quit thread
  16. ManualResetEvent _waitFlag = new ManualResetEvent(true); //for pause purpose
  17. ManualResetEvent _sleepFlag = new ManualResetEvent(false); //for sleep time
  18. object _locker = new object();
  19. #region 属性
  20. /// <summary>
  21. /// 间隔时间
  22. /// </summary>
  23. public int Interval { get { return _interval; } }
  24. #endregion
  25. public PeriodicJob(int interval, Func<bool> func, string name, bool isStartNow=false, bool isBackground=true)
  26. {
  27. _thread = new Thread(new ParameterizedThreadStart(ThreadFunction));
  28. _thread.Name = name;
  29. _thread.IsBackground = isBackground;
  30. LOG.WriteLog(eEvent.EV_SYSTEM_CONFIG, "System", $"Create PeriodJob {name}");
  31. _interval = interval;
  32. _func = func;
  33. _elapseTimer = new DeviceTimer();
  34. if (isStartNow)
  35. Start();
  36. }
  37. public void Start()
  38. {
  39. //lock (_locker)
  40. {
  41. if (_thread == null)
  42. return;
  43. _waitFlag.Set();
  44. if (_thread.IsAlive)
  45. return;
  46. _thread.Start(this);
  47. _elapseTimer.Start(0);
  48. }
  49. }
  50. public void Pause()
  51. {
  52. //lock (_locker)
  53. {
  54. _waitFlag.Reset();
  55. }
  56. }
  57. public void Stop(bool showLog=true)
  58. {
  59. try
  60. {
  61. //lock (_locker)
  62. {
  63. _sleepFlag.Set(); //do not sleep
  64. _waitFlag.Set(); //do not pause
  65. _cancelFlag.Cancel(); //quit
  66. if (_thread == null)
  67. return;
  68. if (_thread.ThreadState != ThreadState.Suspended)
  69. {
  70. try
  71. {
  72. _thread.Abort();
  73. }
  74. catch (Exception ex)
  75. {
  76. if (showLog)
  77. {
  78. LOG.WriteExeption("Thread aborted exception", ex);
  79. }
  80. }
  81. }
  82. _thread = null;
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. LOG.WriteExeption("Thread stop exception", ex);
  88. }
  89. }
  90. void ThreadFunction(object param)
  91. {
  92. PeriodicJob t = (PeriodicJob)param;
  93. t.Run();
  94. }
  95. public void ChangeInterval(int msInterval)
  96. {
  97. _interval = msInterval;
  98. }
  99. void Run()
  100. {
  101. while (!_cancelFlag.IsCancellationRequested)
  102. {
  103. _waitFlag.WaitOne();
  104. _elapseTimer.Start(0);
  105. try
  106. {
  107. if (!_func())
  108. break;
  109. }
  110. catch (Exception ex)
  111. {
  112. LOG.WriteExeption($"{_thread.Name}",ex);
  113. }
  114. _sleepFlag.WaitOne(Math.Max(_interval - (int)_elapseTimer.GetElapseTime(), 30));
  115. }
  116. }
  117. }
  118. }