CancellationToken.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // CancellationToken.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. #if NET_4_0 || MOBILE
  29. namespace System.Threading
  30. {
  31. [System.Diagnostics.DebuggerDisplay ("IsCancellationRequested = {IsCancellationRequested}")]
  32. public struct CancellationToken
  33. {
  34. public CancellationToken (bool canceled)
  35. : this ()
  36. {
  37. // dummy, this is actually set by CancellationTokenSource when the token is created
  38. Source = null;
  39. }
  40. public static CancellationToken None {
  41. get {
  42. return CancellationTokenSource.NoneSource.Token;
  43. }
  44. }
  45. public CancellationTokenRegistration Register (Action callback)
  46. {
  47. return Register (callback, false);
  48. }
  49. public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
  50. {
  51. if (callback == null)
  52. throw new ArgumentNullException ("callback");
  53. return Source.Register (callback, useSynchronizationContext);
  54. }
  55. public CancellationTokenRegistration Register (Action<object> callback, object state)
  56. {
  57. return Register (callback, state, false);
  58. }
  59. public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
  60. {
  61. if (callback == null)
  62. throw new ArgumentNullException ("callback");
  63. return Register (() => callback (state), useSynchronizationContext);
  64. }
  65. public void ThrowIfCancellationRequested ()
  66. {
  67. if (Source.IsCancellationRequested)
  68. throw new OperationCanceledException (this);
  69. }
  70. public bool Equals (CancellationToken other)
  71. {
  72. return this.Source == other.Source;
  73. }
  74. public override bool Equals (object other)
  75. {
  76. return (other is CancellationToken) ? Equals ((CancellationToken)other) : false;
  77. }
  78. public override int GetHashCode ()
  79. {
  80. return Source.GetHashCode ();
  81. }
  82. public static bool operator == (CancellationToken left, CancellationToken right)
  83. {
  84. return left.Equals (right);
  85. }
  86. public static bool operator != (CancellationToken left, CancellationToken right)
  87. {
  88. return !left.Equals (right);
  89. }
  90. public bool CanBeCanceled {
  91. get {
  92. return true;
  93. }
  94. }
  95. public bool IsCancellationRequested {
  96. get {
  97. return Source.IsCancellationRequested;
  98. }
  99. }
  100. public WaitHandle WaitHandle {
  101. get {
  102. return Source.WaitHandle;
  103. }
  104. }
  105. internal CancellationTokenSource Source {
  106. get;
  107. set;
  108. }
  109. }
  110. }
  111. #endif