123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace parkMonitor.tools
- {
- /// <summary>
- /// 计时器类,提供计算时间间隔与判断时长方法
- /// </summary>
- class MyTimer
- {
- public static bool restart { get; set; }
- public bool rolledBack { get; set; }
- private DateTime startTime;
- private DateTime endTime;
- private bool recorded;
- private bool displayed;
- private int activationCount;
- private TimeSpan timeSpan;
- /// <summary>
- /// 构造函数
- /// </summary>
- public MyTimer()
- {
- restart = false;
- recorded = false;
- displayed = false;
- rolledBack = false;
- activationCount = 0;
- }
- /// <summary>
- /// 开始计时
- /// </summary>
- public void StartTiming()
- {
- recorded = false;
- startTime = DateTime.Now;
- }
- /// <summary>
- /// 结束计时
- /// </summary>
- public void EndTiming()
- {
- endTime = DateTime.Now;
- timeSpan = endTime - startTime;
- recorded = true;
- }
- /// <summary>
- /// 获取时间间隔
- /// </summary>
- /// <returns>返回TimeSpan对象</returns>
- public TimeSpan GetInterval()
- {
- if (recorded)
- return timeSpan;
- else
- return new TimeSpan();
- }
- /// <summary>
- /// 判断时长是否在范围内
- /// </summary>
- /// <param name="minCount">最小单位个数</param>
- /// <param name="maxCount">最大单位个数</param>
- /// <param name="secondsInUnit">一单位对应秒数</param>
- /// /// <param name="displayOnlyOnce">是否只在外界输出一次</param>
- /// <returns>判断结果</returns>
- public bool IsInBetween(int minCount, int maxCount, double secondsInUnit, bool displayOnlyOnce)
- {
- TimeSpan ts = GetInterval();
- if (ts.TotalSeconds >= minCount * secondsInUnit && ts.TotalSeconds <= maxCount * secondsInUnit)
- {
- if (displayOnlyOnce && displayed)
- return false;
- else
- {
- displayed = true;
- return true;
- }
- }
- else
- return false;
- }
- /// /// <summary>
- /// 判断时长是否在范围内
- /// </summary>
- /// <param name="minCount">最小单位个数</param>
- /// <param name="secondsInUnit">一单位对应秒数</param>
- /// /// <param name="displayOnlyOnce">是否只在外界激活一次</param>
- /// /// <param name="activationCount">记录被激活次数</param>
- /// <returns>判断结果</returns>
- public bool IsLonger(int minCount, double secondsInUnit, bool displayOnlyOnce, out int activationCount)
- {
- TimeSpan ts = GetInterval();
- activationCount = this.activationCount;
- if (ts.TotalSeconds >= minCount * secondsInUnit)
- {
- if (displayOnlyOnce && displayed)
- return false;
- else
- {
- this.activationCount += 1;
- activationCount = this.activationCount;
- displayed = true;
- return true;
- }
- }
- else
- return false;
- }
- }
- }
|