You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimeBased.cs 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. /// <summary>
  7. /// 记录上次Start的时间,尚未Start则为long.MaxValue
  8. /// 当前不为long.MaxValue则不能Start
  9. /// </summary>
  10. public class StartTime
  11. {
  12. private long _time;
  13. public StartTime(long time)
  14. {
  15. _time = time;
  16. }
  17. public StartTime() { _time = long.MaxValue; }
  18. public long Get() => Interlocked.CompareExchange(ref _time, -2, -2);
  19. public override string ToString() => Get().ToString();
  20. /// <returns>返回操作前的值</returns>
  21. public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
  22. /// <returns>返回操作前的值</returns>
  23. public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
  24. /// <returns>返回时间差,<0意味着未开始</returns>
  25. public long StopIfPassing(long passedTime)
  26. {
  27. long ans = Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2);
  28. if (ans > passedTime)
  29. {
  30. Interlocked.Exchange(ref _time, long.MaxValue);
  31. }
  32. return ans;
  33. }
  34. }
  35. public class LongInTheVariableRangeWithStartTime : LongInTheVariableRange
  36. {
  37. public StartTime startTime = new();
  38. public LongInTheVariableRangeWithStartTime(long value, long maxValue) : base(value, maxValue) { }
  39. /// <summary>
  40. /// 默认使Value=maxValue
  41. /// </summary>
  42. public LongInTheVariableRangeWithStartTime(long maxValue) : base(maxValue) { }
  43. public LongInTheVariableRangeWithStartTime() : base() { }
  44. #region 读取
  45. public (long, long) GetValueWithStartTime()
  46. {
  47. lock (vLock)
  48. {
  49. return (v, startTime.Get());
  50. }
  51. }
  52. public (long, long, long) GetValueAndMaxVWithStartTime()
  53. {
  54. lock (vLock)
  55. {
  56. return (v, maxV, startTime.Get());
  57. }
  58. }
  59. #endregion
  60. /// <summary>
  61. /// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue
  62. /// 如果无法加到maxValue则不加
  63. /// </summary>
  64. /// <returns>返回试图加到的值与最大值</returns>
  65. public (long, long, long) AddStartTimeToMaxV(double speed = 1.0)
  66. {
  67. lock (vLock)
  68. {
  69. long addV = (long)(startTime.StopIfPassing(maxV - v) * speed);
  70. if (addV < 0) return (v, maxV, startTime.Get());
  71. if (maxV - v < addV) return (v = maxV, maxV, startTime.Get());
  72. return (v + addV, maxV, startTime.Get());
  73. }
  74. }
  75. /// <summary>
  76. /// 增加量为时间差*速度,并将startTime变为long.MaxValue
  77. /// </summary>
  78. /// <returns>返回实际改变量</returns>
  79. public long AddStartTime(double speed = 1.0)
  80. {
  81. lock (vLock)
  82. {
  83. long previousV = v;
  84. long addV = (Environment.TickCount64 - startTime.Stop());
  85. if (addV < 0) v += (long)(addV * speed);
  86. else return 0;
  87. if (v > maxV) v = maxV;
  88. return v - previousV;
  89. }
  90. }
  91. /// <summary>
  92. /// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上
  93. /// 如果无法加到maxValue则清零
  94. /// 无论如何startTime变为long.MaxValue
  95. /// </summary>
  96. /// <returns>返回是否清零</returns>
  97. public bool Set0IfNotAddStartTimeToMaxV(double speed = 1.0)
  98. {
  99. lock (vLock)
  100. {
  101. if (v == maxV) return false;
  102. long addV = (long)(startTime.Stop() * speed);
  103. if (addV < 0)
  104. {
  105. v = 0;
  106. return true;
  107. }
  108. if (maxV - v < addV)
  109. {
  110. v = maxV;
  111. return false;
  112. }
  113. v = 0;
  114. return false;
  115. }
  116. }
  117. public void SetAndStop(long value = 0)
  118. {
  119. lock (vLock)
  120. {
  121. this.v = value;
  122. startTime.Stop();
  123. }
  124. }
  125. }
  126. public class TimeBasedProgressAtVariableSpeed
  127. {
  128. private LongInTheVariableRangeWithStartTime progress;
  129. public AtomicDouble speed;
  130. #region 构造
  131. public TimeBasedProgressAtVariableSpeed(long needProgress, double speed = 1.0)
  132. {
  133. progress = new LongInTheVariableRangeWithStartTime(0, needProgress);
  134. if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
  135. this.speed = new(speed);
  136. }
  137. public TimeBasedProgressAtVariableSpeed()
  138. {
  139. progress = new LongInTheVariableRangeWithStartTime(0, 0);
  140. this.speed = new(1.0);
  141. }
  142. #endregion
  143. #region 读取
  144. public override string ToString()
  145. {
  146. long progressStored, lastStartTime;
  147. (progressStored, lastStartTime) = progress.GetValueWithStartTime();
  148. return "ProgressStored: " + progressStored.ToString()
  149. + " ; LastStartTime: " + lastStartTime.ToString() + "ms"
  150. + " ; Speed: " + speed.ToString();
  151. }
  152. public long GetProgressNow() => progress.AddStartTimeToMaxV((double)speed).Item1;
  153. public (long, long, long) GetProgressNowAndNeedTimeAndLastStartTime() => progress.AddStartTimeToMaxV((double)speed);
  154. public long GetProgressStored() => progress.GetValue();
  155. public (long, long) GetProgressStoredAndNeedTime() => progress.GetValueAndMaxV();
  156. public (long, long, long) GetProgressStoredAndNeedTimeAndLastStartTime() => progress.GetValueAndMaxVWithStartTime();
  157. public bool IsFinished()
  158. {
  159. long progressNow, needTime;
  160. (progressNow, needTime, _) = progress.AddStartTimeToMaxV((double)speed);
  161. return progressNow == needTime;
  162. }
  163. public bool IsProgressing()
  164. {
  165. long progressNow, needTime, startT;
  166. (progressNow, needTime, startT) = progress.AddStartTimeToMaxV((double)speed);
  167. return (startT != long.MaxValue && progressNow != needTime);
  168. }
  169. #endregion
  170. public bool Start(long needTime)
  171. {
  172. if (needTime <= 2)
  173. {
  174. Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  175. return false;
  176. }
  177. if (progress.startTime.Start() != long.MaxValue) return false;
  178. progress.SetMaxV(needTime);
  179. return true;
  180. }
  181. public bool Start()
  182. {
  183. return progress.startTime.Start() == long.MaxValue;
  184. }
  185. /// <summary>
  186. /// 使进度条强制终止清零
  187. /// </summary>
  188. public void Set0()
  189. {
  190. progress.SetAndStop();
  191. }
  192. /// <summary>
  193. /// 如果进度条加上时间差不能为满,使进度条强制终止清零
  194. /// </summary>
  195. public void TryStop()
  196. {
  197. progress.Set0IfNotAddStartTimeToMaxV(speed);
  198. }
  199. /// <summary>
  200. /// 使进度条暂停
  201. /// </summary>
  202. public bool Pause()
  203. {
  204. return progress.AddStartTime((double)speed) != 0;
  205. }
  206. /// <summary>
  207. /// 使进度条进度为满
  208. /// </summary>
  209. public void Finish()
  210. {
  211. progress.SetVToMaxV();
  212. progress.startTime.Stop();
  213. }
  214. }
  215. /// <summary>
  216. /// 根据时间推算Start后完成多少进度的进度条(long)。
  217. /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
  218. /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  219. /// 通过原子操作实现。
  220. /// </summary>
  221. public class TimeBasedProgressOptimizedForInterrupting
  222. {
  223. private long endT = long.MaxValue;
  224. private long needT;
  225. public TimeBasedProgressOptimizedForInterrupting(long needTime)
  226. {
  227. if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
  228. this.needT = needTime;
  229. }
  230. public TimeBasedProgressOptimizedForInterrupting()
  231. {
  232. this.needT = 0;
  233. }
  234. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  235. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  236. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  237. public bool IsFinished()
  238. {
  239. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  240. }
  241. public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
  242. /// <summary>
  243. /// GetProgress<0则表明未开始
  244. /// </summary>
  245. public long GetProgress()
  246. {
  247. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  248. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  249. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  250. }
  251. public long GetNonNegativeProgress()
  252. {
  253. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  254. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  255. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  256. return progress < 0 ? 0 : progress;
  257. }
  258. /// <summary>
  259. /// GetProgress<0则表明未开始
  260. /// </summary>
  261. public long GetProgress(long time)
  262. {
  263. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  264. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  265. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  266. }
  267. public long GetNonNegativeProgress(long time)
  268. {
  269. long cutime = Interlocked.Read(ref endT) - time;
  270. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  271. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  272. return progress < 0 ? 0 : progress;
  273. }
  274. /// <summary>
  275. /// <0则表明未开始
  276. /// </summary>
  277. public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();
  278. /// <summary>
  279. /// GetProgressDouble<0则表明未开始
  280. /// </summary>
  281. public double GetProgressDouble()
  282. {
  283. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  284. if (cutime <= 0) return 1;
  285. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  286. if (needTime == 0) return 0;
  287. return 1.0 - ((double)cutime / needTime);
  288. }
  289. public double GetNonNegativeProgressDouble(long time)
  290. {
  291. long cutime = Interlocked.Read(ref endT) - time;
  292. if (cutime <= 0) return 1;
  293. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  294. if (needTime <= cutime) return 0;
  295. return 1.0 - ((double)cutime / needTime);
  296. }
  297. public bool Start(long needTime)
  298. {
  299. if (needTime <= 0)
  300. {
  301. Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  302. return false;
  303. }
  304. //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  305. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  306. if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
  307. Interlocked.Exchange(ref needT, needTime);
  308. return true;
  309. }
  310. public bool Start()
  311. {
  312. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  313. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  314. return true;
  315. }
  316. /// <summary>
  317. /// 使进度条强制终止清零
  318. /// </summary>
  319. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  320. /// <summary>
  321. /// 使未完成的进度条终止清零
  322. /// </summary>
  323. public bool InterruptToSet0()
  324. {
  325. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  326. {
  327. Interlocked.Exchange(ref endT, long.MaxValue);
  328. return true;
  329. }
  330. return false;
  331. }
  332. //增加其他新的写操作可能导致不安全
  333. }
  334. /// <summary>
  335. /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
  336. /// </summary>
  337. public class BoolUpdateEachCD
  338. {
  339. private long cd;
  340. private long nextUpdateTime = 0;
  341. public BoolUpdateEachCD(int cd)
  342. {
  343. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  344. this.cd = cd;
  345. }
  346. public BoolUpdateEachCD(long cd)
  347. {
  348. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  349. this.cd = cd;
  350. }
  351. public BoolUpdateEachCD(long cd, long startTime)
  352. {
  353. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  354. this.cd = cd;
  355. this.nextUpdateTime = startTime;
  356. }
  357. public long GetCD() => Interlocked.Read(ref cd);
  358. public bool TryUse()
  359. {
  360. long needTime = Interlocked.Exchange(ref nextUpdateTime, long.MaxValue);
  361. if (needTime <= Environment.TickCount64)
  362. {
  363. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  364. return true;
  365. }
  366. Interlocked.Exchange(ref nextUpdateTime, needTime);
  367. return false;
  368. }
  369. public void SetCD(int cd)
  370. {
  371. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd to " + cd.ToString() + ".");
  372. Interlocked.Exchange(ref this.cd, cd);
  373. }
  374. }
  375. /// <summary>
  376. /// 冷却时间为可变的CDms的进度条,初始为满
  377. /// </summary>
  378. public class LongProgressUpdateEachCD
  379. {
  380. private int isusing = 0;
  381. private long cd;
  382. private long nextUpdateTime = 0;
  383. public LongProgressUpdateEachCD(int cd)
  384. {
  385. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  386. this.cd = cd;
  387. }
  388. public LongProgressUpdateEachCD(long cd)
  389. {
  390. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  391. this.cd = cd;
  392. }
  393. public LongProgressUpdateEachCD(long cd, long startTime)
  394. {
  395. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  396. this.cd = cd;
  397. this.nextUpdateTime = startTime;
  398. }
  399. public long GetRemainingTime()
  400. {
  401. long v = Interlocked.Read(ref nextUpdateTime) - Environment.TickCount64;
  402. return v < 0 ? 0 : v;
  403. }
  404. public long GetCD() => Interlocked.Read(ref cd);
  405. public bool TryUse()
  406. {
  407. if (Interlocked.Exchange(ref isusing, 1) == 1) return false;
  408. long needTime = Interlocked.Read(ref nextUpdateTime);
  409. if (needTime <= Environment.TickCount64)
  410. {
  411. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  412. Interlocked.Exchange(ref isusing, 0);
  413. return true;
  414. }
  415. Interlocked.Exchange(ref isusing, 0);
  416. return false;
  417. }
  418. public void SetCD(int cd)
  419. {
  420. if (cd <= 1) Debugger.Output("Bug:Set LongProgressUpdateEachCD.cd to " + cd.ToString() + ".");
  421. Interlocked.Exchange(ref this.cd, cd);
  422. }
  423. }
  424. /// <summary>
  425. /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
  426. /// </summary>
  427. public class IntNumUpdateEachCD
  428. {
  429. private int num;
  430. private int maxNum;
  431. private int cd;
  432. private long updateTime = 0;
  433. private readonly object numLock = new();
  434. public IntNumUpdateEachCD(int num, int maxNum, int cd)
  435. {
  436. if (num < 0) Debugger.Output("Bug:IntNumUpdateEachCD.num (" + num.ToString() + ") is less than 0.");
  437. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  438. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  439. this.num = (num < maxNum) ? num : maxNum;
  440. this.maxNum = maxNum;
  441. this.cd = cd;
  442. this.updateTime = Environment.TickCount64;
  443. }
  444. /// <summary>
  445. /// 默认使num=maxNum
  446. /// </summary>
  447. public IntNumUpdateEachCD(int maxNum, int cd)
  448. {
  449. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  450. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  451. this.num = this.maxNum = maxNum;
  452. this.cd = cd;
  453. }
  454. public IntNumUpdateEachCD()
  455. {
  456. this.num = this.maxNum = 0;
  457. this.cd = int.MaxValue;
  458. }
  459. public int GetMaxNum() { lock (numLock) return maxNum; }
  460. public int GetCD() { lock (numLock) return cd; }
  461. public int GetNum(long time)
  462. {
  463. lock (numLock)
  464. {
  465. if (num < maxNum && time - updateTime >= cd)
  466. {
  467. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  468. updateTime += add * cd;
  469. return (num += add);
  470. }
  471. return num;
  472. }
  473. }
  474. public static implicit operator int(IntNumUpdateEachCD aint) => aint.GetNum(Environment.TickCount64);
  475. /// <summary>
  476. /// 应当保证该subV>=0
  477. /// </summary>
  478. public int TrySub(int subV)
  479. {
  480. if (subV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to sub " + subV.ToString() + ", which is less than 0.");
  481. long time = Environment.TickCount64;
  482. lock (numLock)
  483. {
  484. if (num < maxNum && time - updateTime >= cd)
  485. {
  486. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  487. updateTime += add * cd;
  488. num += add;
  489. }
  490. if (num == maxNum) updateTime = time;
  491. num -= subV = Math.Min(subV, num);
  492. }
  493. return subV;
  494. }
  495. /// <summary>
  496. /// 应当保证该addV>=0
  497. /// </summary>
  498. public void TryAdd(int addV)
  499. {
  500. if (addV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to add " + addV.ToString() + ", which is less than 0.");
  501. lock (numLock)
  502. {
  503. num += Math.Min(addV, maxNum - num);
  504. }
  505. }
  506. /// <summary>
  507. /// 若maxNum<=0则maxNum及Num设为0并返回False
  508. /// </summary>
  509. public bool SetMaxNumAndNum(int maxNum)
  510. {
  511. if (maxNum < 0) maxNum = 0;
  512. lock (numLock)
  513. {
  514. this.num = this.maxNum = maxNum;
  515. }
  516. return maxNum > 0;
  517. }
  518. /// <summary>
  519. /// 应当保证该maxnum>=0
  520. /// </summary>
  521. public void SetPositiveMaxNumAndNum(int maxNum)
  522. {
  523. lock (numLock)
  524. {
  525. this.num = this.maxNum = maxNum;
  526. }
  527. }
  528. /// <summary>
  529. /// 应当保证该maxnum>=0
  530. /// </summary>
  531. public void SetPositiveMaxNum(int maxNum)
  532. {
  533. lock (numLock)
  534. {
  535. if ((this.maxNum = maxNum) < num)
  536. num = maxNum;
  537. }
  538. }
  539. /// <summary>
  540. /// 若maxNum<=0则maxNum及Num设为0并返回False
  541. /// </summary>
  542. public bool SetMaxNum(int maxNum)
  543. {
  544. if (maxNum < 0) maxNum = 0;
  545. lock (numLock)
  546. {
  547. if ((this.maxNum = maxNum) < num)
  548. num = maxNum;
  549. }
  550. return maxNum > 0;
  551. }
  552. /// <summary>
  553. /// 若num<0则num设为0并返回False
  554. /// </summary>
  555. public bool SetNum(int num)
  556. {
  557. lock (numLock)
  558. {
  559. if (num < 0)
  560. {
  561. this.num = 0;
  562. updateTime = Environment.TickCount64;
  563. return false;
  564. }
  565. if (num < maxNum)
  566. {
  567. if (this.num == maxNum) updateTime = Environment.TickCount64;
  568. this.num = num;
  569. }
  570. else this.num = maxNum;
  571. return true;
  572. }
  573. }
  574. /// <summary>
  575. /// 应当保证该num>=0
  576. /// </summary>
  577. public void SetPositiveNum(int num)
  578. {
  579. lock (numLock)
  580. {
  581. if (num < maxNum)
  582. {
  583. if (this.num == maxNum) updateTime = Environment.TickCount64;
  584. this.num = num;
  585. }
  586. else this.num = maxNum;
  587. }
  588. }
  589. public void SetCD(int cd)
  590. {
  591. lock (numLock)
  592. {
  593. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
  594. this.cd = cd;
  595. }
  596. }
  597. }
  598. }