ETCancellationTokenSource.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Threading;
  2. namespace ETModel
  3. {
  4. [ObjectSystem]
  5. public class ETCancellationTokenSourceAwakeSystem: AwakeSystem<ETCancellationTokenSource>
  6. {
  7. public override void Awake(ETCancellationTokenSource self)
  8. {
  9. self.CancellationTokenSource = new CancellationTokenSource();
  10. }
  11. }
  12. [ObjectSystem]
  13. public class ETCancellationTokenSourceAwake2System: AwakeSystem<ETCancellationTokenSource, long>
  14. {
  15. public override void Awake(ETCancellationTokenSource self, long afterTimeCancel)
  16. {
  17. self.CancellationTokenSource = new CancellationTokenSource();
  18. self.CancelAfter(afterTimeCancel).Coroutine();
  19. }
  20. }
  21. public class ETCancellationTokenSource: Component
  22. {
  23. public CancellationTokenSource CancellationTokenSource;
  24. public void Cancel()
  25. {
  26. if (this.CancellationTokenSource == null)
  27. {
  28. return;
  29. }
  30. CancellationTokenSource cts = this.CancellationTokenSource;
  31. this.CancellationTokenSource = null;
  32. cts?.Cancel();
  33. }
  34. public async ETVoid CancelAfter(long afterTimeCancel)
  35. {
  36. if (this.CancellationTokenSource == null)
  37. {
  38. return;
  39. }
  40. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(afterTimeCancel);
  41. if (this.CancellationTokenSource == null)
  42. {
  43. return;
  44. }
  45. CancellationTokenSource cts = this.CancellationTokenSource;
  46. this.CancellationTokenSource = null;
  47. cts?.Cancel();
  48. }
  49. public CancellationToken Token
  50. {
  51. get
  52. {
  53. return this.CancellationTokenSource.Token;
  54. }
  55. }
  56. public override void Dispose()
  57. {
  58. if (this.IsDisposed)
  59. {
  60. return;
  61. }
  62. base.Dispose();
  63. this.CancellationTokenSource?.Cancel();
  64. }
  65. }
  66. }