Timing.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using System;
  2. using System.Collections.Generic;
  3. namespace CommonLang
  4. {
  5. public delegate void TickHandler(TimeTaskMS task);
  6. public class TimeInterval<T>
  7. {
  8. public T Tag;
  9. private int mIntervalTimeMS;
  10. private int mPassTimeMS = 0;
  11. private int mTickCount = 0;
  12. private bool mFirstTimeEnable = true;
  13. /// <summary>
  14. /// 间隔时间
  15. /// </summary>
  16. public int IntervalTimeMS { get { return mIntervalTimeMS; } }
  17. /// <summary>
  18. /// 触发过多少次
  19. /// </summary>
  20. public int TotalTickCount { get { return mTickCount; } }
  21. /// <summary>
  22. /// 总共经过时间
  23. /// </summary>
  24. public int TotalPassTimeMS { get { return mPassTimeMS; } }
  25. public bool FirstTimeEnable
  26. {
  27. set
  28. {
  29. mFirstTimeEnable = value;
  30. }
  31. private get
  32. {
  33. return mFirstTimeEnable;
  34. }
  35. }
  36. public TimeInterval(int intervalMS)
  37. {
  38. this.mIntervalTimeMS = intervalMS;
  39. }
  40. public TimeInterval(T tag, int intervalMS)
  41. {
  42. this.mIntervalTimeMS = intervalMS;
  43. this.Tag = tag;
  44. }
  45. public void SetPassTime(int passtimeMS)
  46. {
  47. mPassTimeMS = passtimeMS;
  48. }
  49. /// <summary>
  50. /// 记录归零
  51. /// </summary>
  52. public void Reset()
  53. {
  54. mPassTimeMS = 0;
  55. mTickCount = 0;
  56. }
  57. public void Reset(int intervalMS)
  58. {
  59. mIntervalTimeMS = intervalMS;
  60. mPassTimeMS = 0;
  61. mTickCount = 0;
  62. }
  63. /// <summary>
  64. /// 更新
  65. /// </summary>
  66. /// <param name="intervalMS">间隔时间</param>
  67. /// <returns>是否应该触发</returns>
  68. //public bool Update(int intervalMS)
  69. //{
  70. // if (mIntervalTimeMS > 0)
  71. // {
  72. // if (mIntervalTimeMS <= mPassTimeMS)
  73. // {
  74. // this.mPassTimeMS += intervalMS;
  75. // this.mPassTimeMS -= mIntervalTimeMS;
  76. // this.mTickCount++;
  77. // return true;
  78. // }
  79. // else if (mPassTimeMS == 0 && FirstTimeEnable)
  80. // {
  81. // this.mPassTimeMS += intervalMS;
  82. // return true;
  83. // }
  84. // else
  85. // {
  86. // this.mPassTimeMS += intervalMS;
  87. // }
  88. // }
  89. // return false;
  90. //}
  91. public bool Update(int intervalMS)
  92. {
  93. bool isTrigger = false;
  94. if (mIntervalTimeMS > 0)
  95. {
  96. this.mPassTimeMS += intervalMS;
  97. if (FirstTimeEnable && this.mTickCount == 0)
  98. {
  99. this.mTickCount++;
  100. isTrigger = true;
  101. }
  102. else if(mIntervalTimeMS <= mPassTimeMS)
  103. {
  104. this.mPassTimeMS -= mIntervalTimeMS;
  105. this.mTickCount++;
  106. isTrigger = true;
  107. }
  108. }
  109. return isTrigger;
  110. }
  111. public TimeInterval<T> ChangeTiming(int intervalMs)
  112. {
  113. TimeInterval<T> rlt = new TimeInterval<T>(intervalMs);
  114. rlt.mPassTimeMS = this.mPassTimeMS;
  115. rlt.mTickCount = this.mTickCount;
  116. rlt.Tag = this.Tag;
  117. return rlt;
  118. }
  119. }
  120. public class TimeExpire<T>
  121. {
  122. public T Tag { get; set; }
  123. public int TotalTimeMS { get { return mTotalTimeMS; } }
  124. public int PassTimeMS { get { return mPassTimeMS; } }
  125. public int ExpireTimeMS { get { return mTotalTimeMS - mPassTimeMS; } }
  126. private int mTotalTimeMS;
  127. private int mPassTimeMS;
  128. public TimeExpire(int totalMS)
  129. {
  130. this.mTotalTimeMS = totalMS;
  131. this.mPassTimeMS = 0;
  132. }
  133. public TimeExpire(T tag, int totalMS)
  134. : this(totalMS)
  135. {
  136. this.Tag = tag;
  137. }
  138. /// <summary>
  139. /// 记录归零
  140. /// </summary>
  141. public void Reset()
  142. {
  143. this.mPassTimeMS = 0;
  144. }
  145. public void Reset(int totalTimeMS)
  146. {
  147. this.mPassTimeMS = 0;
  148. this.mTotalTimeMS = totalTimeMS;
  149. }
  150. public void End()
  151. {
  152. this.mPassTimeMS = mTotalTimeMS;
  153. }
  154. /// <summary>
  155. /// 更新
  156. /// </summary>
  157. /// <param name="intervalMS">间隔时间</param>
  158. /// <returns>是否到期</returns>
  159. public bool Update(int intervalMS)
  160. {
  161. if (mPassTimeMS >= mTotalTimeMS)
  162. {
  163. return true;
  164. }
  165. this.mPassTimeMS += intervalMS;
  166. return false;
  167. }
  168. /// <summary>
  169. /// 是否结束
  170. /// </summary>
  171. public bool IsEnd { get { return mPassTimeMS >= mTotalTimeMS; } }
  172. /// <summary>
  173. /// 剩余百分比
  174. /// </summary>
  175. public float Percent
  176. {
  177. get
  178. {
  179. return Math.Min((float)mPassTimeMS / (float)mTotalTimeMS, 1);
  180. }
  181. }
  182. }
  183. public class TimeTaskMS
  184. {
  185. readonly private TickHandler Handler;
  186. readonly private int IntervalTimeMS;
  187. readonly private int DelayTimeMS;
  188. readonly private int RepeatCount;
  189. public object UserData;
  190. private bool mRunning = false;
  191. private bool mExit = false;
  192. private int PassTimeMS = 0;
  193. private int DelayTimer = 0;
  194. private int RepeatTick = 0;
  195. private bool mInvoking = false;
  196. public TimeTaskMS(int intervalMS, int delayMS, int repeat, TickHandler handler)
  197. {
  198. this.Handler = handler;
  199. this.IntervalTimeMS = intervalMS;
  200. this.DelayTimeMS = delayMS;
  201. this.RepeatCount = repeat;
  202. Reset();
  203. }
  204. internal void Start()
  205. {
  206. this.PassTimeMS = 0;
  207. this.mRunning = true;
  208. }
  209. /// <summary>
  210. /// 更新
  211. /// </summary>
  212. /// <param name="intervalMS">间隔时间</param>
  213. internal void Update(int intervalMS)
  214. {
  215. if (!mExit && mRunning)
  216. {
  217. this.PassTimeMS += intervalMS;
  218. if (DelayTimer > 0)
  219. {
  220. if (DelayTimer <= PassTimeMS)
  221. {
  222. mInvoking = true;
  223. PassTimeMS -= DelayTimer;
  224. DelayTimer = 0;
  225. }
  226. else
  227. {
  228. return;
  229. }
  230. }
  231. if (IntervalTimeMS <= PassTimeMS)
  232. {
  233. mInvoking = true;
  234. PassTimeMS -= IntervalTimeMS;
  235. if (RepeatCount > 0)
  236. {
  237. RepeatTick++;
  238. if (RepeatTick >= RepeatCount)
  239. {
  240. mExit = true;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. internal void TryInvoke()
  247. {
  248. if (mInvoking)
  249. {
  250. mInvoking = false;
  251. Handler.Invoke(this);
  252. }
  253. }
  254. public void Reset()
  255. {
  256. this.mRunning = true;
  257. this.mExit = false;
  258. this.PassTimeMS = 0;
  259. this.RepeatTick = 0;
  260. this.DelayTimer = DelayTimeMS;
  261. }
  262. public void Stop()
  263. {
  264. mExit = true;
  265. }
  266. public void Pause()
  267. {
  268. mRunning = false;
  269. }
  270. public void Resume()
  271. {
  272. mRunning = true;
  273. }
  274. public bool IsRunning
  275. {
  276. get { return mRunning; }
  277. }
  278. public bool IsExit
  279. {
  280. get { return mExit; }
  281. }
  282. internal bool IsInvoking
  283. {
  284. get { return mInvoking; }
  285. }
  286. }
  287. public class TimeTaskQueue : IDisposable
  288. {
  289. private LinkedList<TimeTaskMS> mTimeTasks = new LinkedList<TimeTaskMS>();
  290. private List<LinkedListNode<TimeTaskMS>> removed = new List<LinkedListNode<TimeTaskMS>>(2);
  291. private List<TimeTaskMS> invoking = new List<TimeTaskMS>(2);
  292. public void Dispose()
  293. {
  294. mTimeTasks.Clear();
  295. removed.Clear();
  296. invoking.Clear();
  297. }
  298. public void Update(int intervalMS)
  299. {
  300. removed.Clear();
  301. invoking.Clear();
  302. lock (mTimeTasks)
  303. {
  304. for (LinkedListNode<TimeTaskMS> it = mTimeTasks.Last; it != null; it = it.Previous)
  305. {
  306. TimeTaskMS t = it.Value;
  307. t.Update(intervalMS);
  308. if (t.IsInvoking)
  309. {
  310. invoking.Add(t);
  311. }
  312. if (t.IsExit)
  313. {
  314. removed.Add(it);
  315. }
  316. }
  317. if (removed.Count > 0)
  318. {
  319. for (int i = removed.Count - 1; i >= 0; --i)
  320. {
  321. mTimeTasks.Remove(removed[i]);
  322. }
  323. }
  324. }
  325. if (invoking.Count > 0)
  326. {
  327. for (int i = invoking.Count - 1; i >= 0; --i)
  328. {
  329. TimeTaskMS task = invoking[i];
  330. task.TryInvoke();
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// 增加时间任务
  336. /// </summary>
  337. /// <param name="intervalMS"></param>
  338. /// <param name="delayMS"></param>
  339. /// <param name="repeat"></param>
  340. /// <param name="handler"></param>
  341. public TimeTaskMS AddTimeTask(int intervalMS, int delayMS, int repeat, TickHandler handler)
  342. {
  343. TimeTaskMS time = new TimeTaskMS(intervalMS, delayMS, repeat, handler);
  344. lock (mTimeTasks)
  345. {
  346. mTimeTasks.AddLast(time);
  347. }
  348. time.Start();
  349. return time;
  350. }
  351. /// <summary>
  352. /// 增加延时回调方法
  353. /// </summary>
  354. /// <param name="delayMS"></param>
  355. /// <param name="handler"></param>
  356. public TimeTaskMS AddTimeDelayMS(int delayMS, TickHandler handler)
  357. {
  358. TimeTaskMS time = new TimeTaskMS(0, delayMS, 1, handler);
  359. lock (mTimeTasks)
  360. {
  361. mTimeTasks.AddLast(time);
  362. }
  363. time.Start();
  364. return time;
  365. }
  366. /// <summary>
  367. /// 增加定时回调方法
  368. /// </summary>
  369. /// <param name="intervalMS"></param>
  370. /// <param name="handler"></param>
  371. public TimeTaskMS AddTimePeriodicMS(int intervalMS, TickHandler handler)
  372. {
  373. TimeTaskMS time = new TimeTaskMS(intervalMS, 0, 0, handler);
  374. lock (mTimeTasks)
  375. {
  376. mTimeTasks.AddLast(time);
  377. }
  378. time.Start();
  379. return time;
  380. }
  381. }
  382. public delegate void MultiTimeLineRefresh(bool isAdd);
  383. /// <summary>
  384. /// 同时执行多个Timer
  385. /// </summary>
  386. public class MultiTimeLine
  387. {
  388. private readonly List<TimeExpire<int>> Times = new List<TimeExpire<int>>();
  389. private readonly MultiTimeLineRefresh mRefeshNotify;
  390. public MultiTimeLine(MultiTimeLineRefresh notify)
  391. {
  392. this.mRefeshNotify = notify;
  393. }
  394. public TimeExpire<int> Add(int timeMS)
  395. {
  396. TimeExpire<int> ret = new TimeExpire<int>(timeMS);
  397. Times.Add(ret);
  398. if(this.mRefeshNotify != null)
  399. {
  400. this.mRefeshNotify.Invoke(true);
  401. }
  402. return ret;
  403. }
  404. public bool Remove(TimeExpire<int> task)
  405. {
  406. task.End();
  407. if (this.mRefeshNotify != null)
  408. {
  409. this.mRefeshNotify.Invoke(false);
  410. }
  411. return Times.Remove(task);
  412. }
  413. public void Clear()
  414. {
  415. for (int i = Times.Count - 1; i >= 0; --i)
  416. {
  417. TimeExpire<int> task = Times[i];
  418. task.End();
  419. }
  420. Times.Clear();
  421. }
  422. public bool Update(int intervalMS)
  423. {
  424. for (int i = Times.Count - 1; i >= 0; --i)
  425. {
  426. TimeExpire<int> task = Times[i];
  427. if (task.Update(intervalMS))
  428. {
  429. Times.RemoveAt(i);
  430. }
  431. }
  432. return Times.Count > 0;
  433. }
  434. public bool Enable { get { return Times.Count > 0; } }
  435. }
  436. //--------------------------------------------------------------------------------------------
  437. #region SystemTime
  438. public class SystemTimeInterval<T> : TimeInterval<T>
  439. {
  440. private long lastUpdateTime = CUtils.CurrentTimeMS;
  441. public SystemTimeInterval(int intervalMS) : base(intervalMS) { }
  442. public bool Update()
  443. {
  444. long curTime = CUtils.CurrentTimeMS;
  445. int interval = (int)(curTime - lastUpdateTime);
  446. lastUpdateTime = curTime;
  447. return base.Update(interval);
  448. }
  449. }
  450. public class SystemTimeExpire<T> : TimeExpire<T>
  451. {
  452. private long lastUpdateTime = CUtils.CurrentTimeMS;
  453. public SystemTimeExpire(int timeMS) : base(timeMS) { }
  454. public bool Update()
  455. {
  456. long curTime = CUtils.CurrentTimeMS;
  457. int interval = (int)(curTime - lastUpdateTime);
  458. lastUpdateTime = curTime;
  459. return base.Update(interval);
  460. }
  461. }
  462. public class SystemTimeTaskQueue : TimeTaskQueue
  463. {
  464. private long lastUpdateTime = CUtils.CurrentTimeMS;
  465. public void Update()
  466. {
  467. long curTime = CUtils.CurrentTimeMS;
  468. int interval = (int)(curTime - lastUpdateTime);
  469. lastUpdateTime = curTime;
  470. base.Update(interval);
  471. }
  472. }
  473. public class SystemMultiTimeLine : MultiTimeLine
  474. {
  475. private long lastUpdateTime = CUtils.CurrentTimeMS;
  476. public SystemMultiTimeLine() : base(null) { }
  477. public void Update()
  478. {
  479. long curTime = CUtils.CurrentTimeMS;
  480. int interval = (int)(curTime - lastUpdateTime);
  481. lastUpdateTime = curTime;
  482. base.Update(interval);
  483. }
  484. }
  485. #endregion
  486. //--------------------------------------------------------------------------------------------
  487. public class SystemTimeRecoder
  488. {
  489. private long last_update_time;
  490. private int current_interval;
  491. public long LastUpdateTimeMS { get { return last_update_time; } }
  492. public int CurrentIntervalMS { get { return current_interval; } }
  493. public SystemTimeRecoder()
  494. {
  495. this.last_update_time = CUtils.CurrentTimeMS;
  496. this.current_interval = 0;
  497. }
  498. public void Reset()
  499. {
  500. this.last_update_time = CUtils.CurrentTimeMS;
  501. this.current_interval = 0;
  502. }
  503. public int Update()
  504. {
  505. long curtime = CUtils.CurrentTimeMS;
  506. current_interval =(int)(curtime - last_update_time);
  507. return current_interval;
  508. }
  509. }
  510. public class TimeUtil
  511. {
  512. //使用替代方案: CommonLang.CUtils.localTimeMS
  513. public static long GetTimestampMS()
  514. {
  515. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  516. return (long)ts.TotalMilliseconds;
  517. //return CommonLang.CUtils.CurrentTimeMS;
  518. }
  519. }
  520. }