Timing.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. 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 Dispose()
  267. {
  268. mExit = true;
  269. this.Handler = null;
  270. }
  271. public void Pause()
  272. {
  273. mRunning = false;
  274. }
  275. public void Resume()
  276. {
  277. mRunning = true;
  278. }
  279. public bool IsRunning
  280. {
  281. get { return mRunning; }
  282. }
  283. public bool IsExit
  284. {
  285. get { return mExit; }
  286. }
  287. internal bool IsInvoking
  288. {
  289. get { return mInvoking; }
  290. }
  291. }
  292. public class TimeTaskQueue : IDisposable
  293. {
  294. private LinkedList<TimeTaskMS> mTimeTasks = new LinkedList<TimeTaskMS>();
  295. private List<LinkedListNode<TimeTaskMS>> removed = new List<LinkedListNode<TimeTaskMS>>(2);
  296. private List<TimeTaskMS> invoking = new List<TimeTaskMS>(2);
  297. public void Dispose()
  298. {
  299. mTimeTasks.Clear();
  300. removed.Clear();
  301. invoking.Clear();
  302. }
  303. public void Update(int intervalMS)
  304. {
  305. removed.Clear();
  306. invoking.Clear();
  307. lock (mTimeTasks)
  308. {
  309. for (LinkedListNode<TimeTaskMS> it = mTimeTasks.Last; it != null; it = it.Previous)
  310. {
  311. TimeTaskMS t = it.Value;
  312. t.Update(intervalMS);
  313. if (t.IsInvoking)
  314. {
  315. invoking.Add(t);
  316. }
  317. if (t.IsExit)
  318. {
  319. removed.Add(it);
  320. }
  321. }
  322. if (removed.Count > 0)
  323. {
  324. for (int i = removed.Count - 1; i >= 0; --i)
  325. {
  326. mTimeTasks.Remove(removed[i]);
  327. }
  328. }
  329. }
  330. if (invoking.Count > 0)
  331. {
  332. for (int i = invoking.Count - 1; i >= 0; --i)
  333. {
  334. TimeTaskMS task = invoking[i];
  335. task.TryInvoke();
  336. }
  337. }
  338. // 手动调用一下dispose
  339. if (removed.Count > 0)
  340. {
  341. for (int i = removed.Count - 1; i >= 0; --i)
  342. {
  343. removed[i].Value.Dispose();
  344. }
  345. }
  346. }
  347. /// <summary>
  348. /// 增加时间任务
  349. /// </summary>
  350. /// <param name="intervalMS"></param>
  351. /// <param name="delayMS"></param>
  352. /// <param name="repeat"></param>
  353. /// <param name="handler"></param>
  354. public TimeTaskMS AddTimeTask(int intervalMS, int delayMS, int repeat, TickHandler handler)
  355. {
  356. TimeTaskMS time = new TimeTaskMS(intervalMS, delayMS, repeat, handler);
  357. lock (mTimeTasks)
  358. {
  359. mTimeTasks.AddLast(time);
  360. }
  361. time.Start();
  362. return time;
  363. }
  364. /// <summary>
  365. /// 增加延时回调方法
  366. /// </summary>
  367. /// <param name="delayMS"></param>
  368. /// <param name="handler"></param>
  369. public TimeTaskMS AddTimeDelayMS(int delayMS, TickHandler handler)
  370. {
  371. TimeTaskMS time = new TimeTaskMS(0, delayMS, 1, handler);
  372. lock (mTimeTasks)
  373. {
  374. mTimeTasks.AddLast(time);
  375. }
  376. time.Start();
  377. return time;
  378. }
  379. /// <summary>
  380. /// 增加定时回调方法
  381. /// </summary>
  382. /// <param name="intervalMS"></param>
  383. /// <param name="handler"></param>
  384. public TimeTaskMS AddTimePeriodicMS(int intervalMS, TickHandler handler)
  385. {
  386. TimeTaskMS time = new TimeTaskMS(intervalMS, 0, 0, handler);
  387. lock (mTimeTasks)
  388. {
  389. mTimeTasks.AddLast(time);
  390. }
  391. time.Start();
  392. return time;
  393. }
  394. }
  395. public delegate void MultiTimeLineRefresh(bool isAdd);
  396. /// <summary>
  397. /// 同时执行多个Timer
  398. /// </summary>
  399. public class MultiTimeLine
  400. {
  401. private List<TimeExpire<int>> Times = new List<TimeExpire<int>>();
  402. private MultiTimeLineRefresh mRefeshNotify;
  403. public MultiTimeLine(MultiTimeLineRefresh notify)
  404. {
  405. this.mRefeshNotify = notify;
  406. }
  407. public TimeExpire<int> Add(int timeMS)
  408. {
  409. TimeExpire<int> ret = new TimeExpire<int>(timeMS);
  410. Times.Add(ret);
  411. if(this.mRefeshNotify != null)
  412. {
  413. this.mRefeshNotify.Invoke(true);
  414. }
  415. return ret;
  416. }
  417. public bool Remove(TimeExpire<int> task)
  418. {
  419. task.End();
  420. if (this.mRefeshNotify != null)
  421. {
  422. this.mRefeshNotify.Invoke(false);
  423. }
  424. return Times.Remove(task);
  425. }
  426. public void Clear()
  427. {
  428. for (int i = Times.Count - 1; i >= 0; --i)
  429. {
  430. TimeExpire<int> task = Times[i];
  431. task.End();
  432. }
  433. Times.Clear();
  434. }
  435. public void Disposing()
  436. {
  437. this.Clear();
  438. this.mRefeshNotify = null;
  439. }
  440. public bool Update(int intervalMS)
  441. {
  442. for (int i = Times.Count - 1; i >= 0; --i)
  443. {
  444. TimeExpire<int> task = Times[i];
  445. if (task.Update(intervalMS))
  446. {
  447. Times.RemoveAt(i);
  448. }
  449. }
  450. return Times.Count > 0;
  451. }
  452. public bool Enable { get { return Times.Count > 0; } }
  453. }
  454. //--------------------------------------------------------------------------------------------
  455. #region SystemTime
  456. public class SystemTimeInterval<T> : TimeInterval<T>
  457. {
  458. private long lastUpdateTime = CUtils.CurrentTimeMS;
  459. public SystemTimeInterval(int intervalMS) : base(intervalMS) { }
  460. public bool Update()
  461. {
  462. long curTime = CUtils.CurrentTimeMS;
  463. int interval = (int)(curTime - lastUpdateTime);
  464. lastUpdateTime = curTime;
  465. return base.Update(interval);
  466. }
  467. }
  468. public class SystemTimeExpire<T> : TimeExpire<T>
  469. {
  470. private long lastUpdateTime = CUtils.CurrentTimeMS;
  471. public SystemTimeExpire(int timeMS) : base(timeMS) { }
  472. public bool Update()
  473. {
  474. long curTime = CUtils.CurrentTimeMS;
  475. int interval = (int)(curTime - lastUpdateTime);
  476. lastUpdateTime = curTime;
  477. return base.Update(interval);
  478. }
  479. }
  480. public class SystemTimeTaskQueue : TimeTaskQueue
  481. {
  482. private long lastUpdateTime = CUtils.CurrentTimeMS;
  483. public void Update()
  484. {
  485. long curTime = CUtils.CurrentTimeMS;
  486. int interval = (int)(curTime - lastUpdateTime);
  487. lastUpdateTime = curTime;
  488. base.Update(interval);
  489. }
  490. }
  491. //public class SystemMultiTimeLine : MultiTimeLine
  492. //{
  493. // private long lastUpdateTime = CUtils.CurrentTimeMS;
  494. // public SystemMultiTimeLine() : base(null) { }
  495. // public void Update()
  496. // {
  497. // long curTime = CUtils.CurrentTimeMS;
  498. // int interval = (int)(curTime - lastUpdateTime);
  499. // lastUpdateTime = curTime;
  500. // base.Update(interval);
  501. // }
  502. //}
  503. #endregion
  504. //--------------------------------------------------------------------------------------------
  505. public class SystemTimeRecoder
  506. {
  507. private long last_update_time;
  508. private int current_interval;
  509. public long LastUpdateTimeMS { get { return last_update_time; } }
  510. public int CurrentIntervalMS { get { return current_interval; } }
  511. public SystemTimeRecoder()
  512. {
  513. this.last_update_time = CUtils.CurrentTimeMS;
  514. this.current_interval = 0;
  515. }
  516. public void Reset()
  517. {
  518. this.last_update_time = CUtils.CurrentTimeMS;
  519. this.current_interval = 0;
  520. }
  521. public int Update()
  522. {
  523. long curtime = CUtils.CurrentTimeMS;
  524. current_interval =(int)(curtime - last_update_time);
  525. return current_interval;
  526. }
  527. }
  528. public class TimeUtil
  529. {
  530. //使用替代方案: CommonLang.CUtils.localTimeMS
  531. public static long GetTimestampMS()
  532. {
  533. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  534. return (long)ts.TotalMilliseconds;
  535. //return CommonLang.CUtils.CurrentTimeMS;
  536. }
  537. }
  538. }