PeriodicJob.cs 3.8 KB

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