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.

SafeValue.cs 24 kB

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