DeviceTimer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.Util
  8. {
  9. /// <summary>
  10. /// DeviceTimer类
  11. ///
  12. /// 设计目的:
  13. /// 1. 进行ms级别的定时;
  14. /// 2. 弥补使用Windows系统时间作为定时,如果系统时间被更改,定时器面临误判风险(Alpha机台已有多次发生类似事件);
  15. ///
  16. /// 使用场合:
  17. /// 系统中凡是需要定时完成的事件,建议采用该定时器完成定时功能;
  18. /// </summary>
  19. public class DeviceTimer
  20. {
  21. //引入高性能计数器API,通过对CPU计数完成计时
  22. [DllImport("Kernel32.dll")]
  23. private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
  24. //获取当前CPU的工作频率
  25. [DllImport("Kernel32.dll")]
  26. private static extern bool QueryPerformanceFrequency(out long lpFrequency);
  27. /// <summary>
  28. /// 定时器内部状态定义
  29. /// </summary>
  30. private enum DeviceTimerState
  31. {
  32. TM_ST_IDLE = 0,
  33. TM_ST_BUSY = 1,
  34. TM_ST_TIMEOUT = 2,
  35. }
  36. /// <summary>
  37. /// 定时器内部状态
  38. /// </summary>
  39. DeviceTimerState _state;
  40. /// <summary>
  41. /// 定时器开始计时时刻的相对时间点
  42. /// </summary>
  43. long _startTime;
  44. /// <summary>
  45. /// 定时器超时时刻的相对时间点
  46. /// </summary>
  47. long _timeOut;
  48. /// <summary>
  49. /// CPU运行的时钟频率
  50. /// </summary>
  51. double _freq;
  52. /// <summary>
  53. /// 定时时间(单位:ms)
  54. /// </summary>
  55. double _duration;
  56. /// <summary>
  57. /// class constructure
  58. /// </summary>
  59. public DeviceTimer()
  60. {
  61. long freq;
  62. if (QueryPerformanceFrequency(out freq) == false)
  63. throw new Exception("本计算机不支持高性能计数器");
  64. //得到每1ms的CPU计时TickCount数目
  65. _freq = (double)freq / 1000.0;
  66. SetState(DeviceTimerState.TM_ST_IDLE);
  67. QueryPerformanceCounter(out _startTime);
  68. _timeOut = _startTime;
  69. _duration = 0;
  70. }
  71. /// <summary>
  72. /// 内部调用:设置定时器当前状态
  73. /// </summary>
  74. /// <param name="state"></param>
  75. private void SetState(DeviceTimerState state)
  76. {
  77. _state = state;
  78. }
  79. /// <summary>
  80. /// 内部调用:返回定时器当前状态
  81. /// </summary>
  82. /// <returns></returns>
  83. private DeviceTimerState GetState()
  84. {
  85. return _state;
  86. }
  87. /// <summary>
  88. /// 定时器开始计时到现在已流逝的时间(单位:毫秒)
  89. /// </summary>
  90. /// <returns></returns>
  91. public double GetElapseTime()
  92. {
  93. long curCount;
  94. QueryPerformanceCounter(out curCount);
  95. return (double)(curCount - _startTime) / (double)_freq;
  96. }
  97. /// <summary>
  98. /// 获取定时总时间
  99. /// </summary>
  100. /// <returns></returns>
  101. public double GetTotalTime()
  102. {
  103. return _duration;
  104. }
  105. /// <summary>
  106. /// 停止计时器计时
  107. /// </summary>
  108. public void Stop()
  109. {
  110. SetState(DeviceTimerState.TM_ST_IDLE);
  111. QueryPerformanceCounter(out _startTime);
  112. _timeOut = _startTime;
  113. _duration = 0;
  114. }
  115. /// <summary>
  116. /// 启动定时器
  117. /// </summary>
  118. /// <param name="delay_ms">定时时间(单位:毫秒)</param>
  119. public void Start(double delay_ms)
  120. {
  121. QueryPerformanceCounter(out _startTime);
  122. _timeOut = Convert.ToInt64(_startTime + delay_ms * _freq);
  123. SetState(DeviceTimerState.TM_ST_BUSY);
  124. _duration = delay_ms;
  125. }
  126. /// <summary>
  127. /// 重新开始定时器
  128. /// 开始的计时时间以上一次Start的时间为准
  129. /// </summary>
  130. /// <param name="delay_ms">定时时间(单位:毫秒)</param>
  131. public void Restart(double delay_ms)
  132. {
  133. _timeOut = Convert.ToInt64(_startTime + delay_ms * _freq);
  134. SetState(DeviceTimerState.TM_ST_BUSY);
  135. _duration = delay_ms;
  136. }
  137. /// <summary>
  138. /// 返回定时器是否超时
  139. /// </summary>
  140. /// <returns></returns>
  141. public bool IsTimeout()
  142. {
  143. if (_state == DeviceTimerState.TM_ST_IDLE)
  144. {
  145. //System.Diagnostics.Debug.WriteLine("Warning: Misuage of the device timer. You must start it first before you can use it.");
  146. //System.Diagnostics.Debug.Assert(false, "Warning: Misuage of the device timer. You must start it first before you can use it.");
  147. }
  148. long curCount;
  149. QueryPerformanceCounter(out curCount);
  150. if (_state == DeviceTimerState.TM_ST_BUSY && (curCount >= _timeOut))
  151. {
  152. SetState(DeviceTimerState.TM_ST_TIMEOUT);
  153. return true;
  154. }
  155. else if (_state == DeviceTimerState.TM_ST_TIMEOUT)
  156. {
  157. return true;
  158. }
  159. return false;
  160. }
  161. /// <summary>
  162. /// 定时器是否在工作中
  163. /// </summary>
  164. /// <returns></returns>
  165. public bool IsIdle()
  166. {
  167. return (_state == DeviceTimerState.TM_ST_IDLE);
  168. }
  169. }
  170. }