DeviceTimer.cs 5.4 KB

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