MyTimer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace parkMonitor.tools
  7. {
  8. /// <summary>
  9. /// 计时器类,提供计算时间间隔与判断时长方法
  10. /// </summary>
  11. class MyTimer
  12. {
  13. public static bool restart { get; set; }
  14. public bool rolledBack { get; set; }
  15. private DateTime startTime;
  16. private DateTime endTime;
  17. private bool recorded;
  18. private bool displayed;
  19. private int activationCount;
  20. private TimeSpan timeSpan;
  21. /// <summary>
  22. /// 构造函数
  23. /// </summary>
  24. public MyTimer()
  25. {
  26. restart = false;
  27. recorded = false;
  28. displayed = false;
  29. rolledBack = false;
  30. activationCount = 0;
  31. }
  32. /// <summary>
  33. /// 开始计时
  34. /// </summary>
  35. public void StartTiming()
  36. {
  37. recorded = false;
  38. startTime = DateTime.Now;
  39. }
  40. /// <summary>
  41. /// 结束计时
  42. /// </summary>
  43. public void EndTiming()
  44. {
  45. endTime = DateTime.Now;
  46. timeSpan = endTime - startTime;
  47. recorded = true;
  48. }
  49. /// <summary>
  50. /// 获取时间间隔
  51. /// </summary>
  52. /// <returns>返回TimeSpan对象</returns>
  53. public TimeSpan GetInterval()
  54. {
  55. if (recorded)
  56. return timeSpan;
  57. else
  58. return new TimeSpan();
  59. }
  60. /// <summary>
  61. /// 判断时长是否在范围内
  62. /// </summary>
  63. /// <param name="minCount">最小单位个数</param>
  64. /// <param name="maxCount">最大单位个数</param>
  65. /// <param name="secondsInUnit">一单位对应秒数</param>
  66. /// /// <param name="displayOnlyOnce">是否只在外界输出一次</param>
  67. /// <returns>判断结果</returns>
  68. public bool IsInBetween(int minCount, int maxCount, double secondsInUnit, bool displayOnlyOnce)
  69. {
  70. TimeSpan ts = GetInterval();
  71. if (ts.TotalSeconds >= minCount * secondsInUnit && ts.TotalSeconds <= maxCount * secondsInUnit)
  72. {
  73. if (displayOnlyOnce && displayed)
  74. return false;
  75. else
  76. {
  77. displayed = true;
  78. return true;
  79. }
  80. }
  81. else
  82. return false;
  83. }
  84. /// /// <summary>
  85. /// 判断时长是否在范围内
  86. /// </summary>
  87. /// <param name="minCount">最小单位个数</param>
  88. /// <param name="secondsInUnit">一单位对应秒数</param>
  89. /// /// <param name="displayOnlyOnce">是否只在外界激活一次</param>
  90. /// /// <param name="activationCount">记录被激活次数</param>
  91. /// <returns>判断结果</returns>
  92. public bool IsLonger(int minCount, double secondsInUnit, bool displayOnlyOnce, out int activationCount)
  93. {
  94. TimeSpan ts = GetInterval();
  95. activationCount = this.activationCount;
  96. if (ts.TotalSeconds >= minCount * secondsInUnit)
  97. {
  98. if (displayOnlyOnce && displayed)
  99. return false;
  100. else
  101. {
  102. this.activationCount += 1;
  103. activationCount = this.activationCount;
  104. displayed = true;
  105. return true;
  106. }
  107. }
  108. else
  109. return false;
  110. }
  111. }
  112. }