|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace System.Threading |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Propagates notification that operation should be cancelled. |
| 7 | + /// </summary> |
| 8 | + public struct CancellationToken |
| 9 | + { |
| 10 | + CancellationTokenSource source; |
| 11 | + /// <summary> |
| 12 | + /// Initializes the System.Threading.CancellationToken. |
| 13 | + /// </summary> |
| 14 | + /// <param name="source">The canceled state for the token.</param> |
| 15 | + internal CancellationToken(CancellationTokenSource source) |
| 16 | + { |
| 17 | + this.source = source; |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes the System.Threading.CancellationToken. |
| 22 | + /// </summary> |
| 23 | + /// <param name="canceled">The canceled state for the token.</param> |
| 24 | + public CancellationToken(bool canceled) |
| 25 | + { |
| 26 | + this.source = null; |
| 27 | + } |
| 28 | + |
| 29 | + static readonly CancellationToken none = new CancellationToken(); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Returns an empty System.Threading.CancellationToken value. |
| 33 | + /// </summary> |
| 34 | + /// <returns>An empty cancellation token.</returns> |
| 35 | + public static CancellationToken None => none; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets whether cancellation has been requested for this token. |
| 39 | + /// </summary> |
| 40 | + /// <returns>true if cancellation has been requested for this token; otherwise, false.</returns> |
| 41 | + public bool IsCancellationRequested => source.IsCancellationRequested; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets whether this token is capable of being in the canceled state. |
| 45 | + /// </summary> |
| 46 | + /// <returns> true if this token is capable of being in the canceled state; otherwise, false.</returns> |
| 47 | + public bool CanBeCanceled => !source.IsCancellationRequested; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets a System.Threading.WaitHandle that is signaled when the token is canceled. |
| 51 | + /// </summary> |
| 52 | + /// <returns>A System.Threading.WaitHandle that is signaled when the token is canceled.</returns> |
| 53 | + public WaitHandle WaitHandle => source.WaitHandle; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Determines whether the current System.Threading.CancellationToken instance is equal to the specified token. |
| 57 | + /// </summary> |
| 58 | + /// <param name="other"></param> |
| 59 | + /// <returns> |
| 60 | + /// true if the instances are equal; otherwise, false. Two tokens are equal if they |
| 61 | + /// are associated with the same System.Threading.CancellationTokenSource or if they |
| 62 | + /// were both constructed from public System.Threading.CancellationToken constructors |
| 63 | + /// and their System.Threading.CancellationToken.IsCancellationRequested values are |
| 64 | + /// equal. |
| 65 | + /// </returns> |
| 66 | + /// <exception cref="System.ObjectDisposedException">An associated System.Threading.CancellationTokenSource has been disposed.</exception> |
| 67 | + public bool Equals(CancellationToken other) |
| 68 | + { |
| 69 | + if (this.source == other.source) |
| 70 | + return true; |
| 71 | + return false; |
| 72 | + } |
| 73 | + /// <summary> |
| 74 | + /// Determines whether the current System.Threading.CancellationToken instance is equal to the specified System.Object. |
| 75 | + /// </summary> |
| 76 | + /// <param name="other">The other object to which to compare this instance.</param> |
| 77 | + /// <returns> |
| 78 | + /// true if other is a System.Threading.CancellationToken and if the two instances |
| 79 | + /// are equal; otherwise, false. Two tokens are equal if they are associated with |
| 80 | + /// the same System.Threading.CancellationTokenSource or if they were both constructed |
| 81 | + /// from public System.Threading.CancellationToken constructors and their System.Threading.CancellationToken.IsCancellationRequested |
| 82 | + /// values are equal. |
| 83 | + /// </returns> |
| 84 | + /// <exception cref="System.ObjectDisposedException">An associated System.Threading.CancellationTokenSource has been disposed.</exception> |
| 85 | + public override bool Equals(object other) |
| 86 | + { |
| 87 | + if (other is CancellationToken token) |
| 88 | + { |
| 89 | + return Equals(token); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Serves as a hash function for a System.Threading.CancellationToken. |
| 96 | + /// </summary> |
| 97 | + /// <returns>A hash code for the current System.Threading.CancellationToken instance.</returns> |
| 98 | + public override int GetHashCode() |
| 99 | + { |
| 100 | + return source.GetHashCode(); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Registers a delegate that will be called when this System.Threading.CancellationToken is canceled. |
| 105 | + /// </summary> |
| 106 | + /// <param name="callback">The delegate to be executed when the System.Threading.CancellationToken is canceled.</param> |
| 107 | + /// <returns>The System.Threading.CancellationTokenRegistration instance that can be used to deregister the callback.</returns> |
| 108 | + /// <exception cref="System.ArgumentNullException">callback is null</exception> |
| 109 | + public CancellationTokenRegistration Register(Action callback) |
| 110 | + { |
| 111 | + return Register(callback, true); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Registers a delegate that will be called when this System.Threading.CancellationToken is canceled. |
| 116 | + /// </summary> |
| 117 | + /// <param name="callback">The delegate to be executed when the System.Threading.CancellationToken is canceled.</param> |
| 118 | + /// <param name="useSynchronizationContext">A value that indicates whether to capture the current System.Threading.SynchronizationContext and use it when invoking the callback.</param> |
| 119 | + /// <returns>The System.Threading.CancellationTokenRegistration instance that can be used to deregister the callback.</returns> |
| 120 | + /// <exception cref="System.ArgumentNullException">callback is null</exception> |
| 121 | + public CancellationTokenRegistration Register(Action callback, bool useSynchronizationContext) |
| 122 | + { |
| 123 | + callback = callback ?? throw new ArgumentNullException("callback"); |
| 124 | + if (source.IsCancellationRequested) |
| 125 | + { |
| 126 | + callback(); |
| 127 | + } |
| 128 | + else if (source != null) |
| 129 | + { |
| 130 | + return source.NotifyOnCancelled(callback, useSynchronizationContext); |
| 131 | + } |
| 132 | + return new CancellationTokenRegistration(); |
| 133 | + } |
| 134 | + /// <summary> |
| 135 | + /// Registers a delegate that will be called when this System.Threading.CancellationToken is canceled. |
| 136 | + /// </summary> |
| 137 | + /// <param name="callback">The delegate to be executed when the System.Threading.CancellationToken is canceled.</param> |
| 138 | + /// <param name="state">The state to pass to the callback when the delegate is invoked. This may be null.</param> |
| 139 | + /// <returns>The System.Threading.CancellationTokenRegistration instance that can be used to deregister the callback.</returns> |
| 140 | + /// <exception cref="System.ArgumentNullException">callback is null</exception> |
| 141 | + public CancellationTokenRegistration Register(Action<object> callback, object state) |
| 142 | + { |
| 143 | + return Register(() => callback(state), true); |
| 144 | + } |
| 145 | + /// <summary> |
| 146 | + /// Registers a delegate that will be called when this System.Threading.CancellationToken is canceled. |
| 147 | + /// </summary> |
| 148 | + /// <param name="callback">The delegate to be executed when the System.Threading.CancellationToken is canceled.</param> |
| 149 | + /// <param name="state">The state to pass to the callback when the delegate is invoked. This may be null.</param> |
| 150 | + /// <param name="useSynchronizationContext">A Boolean value that indicates whether to capture the current System.Threading.SynchronizationContext and use it when invoking the callback.</param> |
| 151 | + /// <returns>The System.Threading.CancellationTokenRegistration instance that can be used to deregister the callback.</returns> |
| 152 | + /// <exception cref="System.ArgumentNullException">callback is null</exception> |
| 153 | + public CancellationTokenRegistration Register(Action<object> callback, object state, bool useSynchronizationContext) |
| 154 | + { |
| 155 | + return Register(() => callback(state), useSynchronizationContext); |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Throws a System.OperationCanceledException if this token has had cancellation requested |
| 160 | + /// </summary> |
| 161 | + public void ThrowIfCancellationRequested() |
| 162 | + { |
| 163 | + if (source.IsCancellationRequested) |
| 164 | + throw new SystemException();//TODO: throw OperationCanceledException, not defined yet |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Determines whether two System.Threading.CancellationToken instances are equal. |
| 169 | + /// </summary> |
| 170 | + /// <param name="left">The first instance.</param> |
| 171 | + /// <param name="right">The second instance.</param> |
| 172 | + /// <returns> true if the instances are equal; otherwise, false.</returns> |
| 173 | + public static bool operator ==(CancellationToken left, CancellationToken right) |
| 174 | + { |
| 175 | + return left.Equals(right); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Determines whether two System.Threading.CancellationToken instances are not equal. |
| 180 | + /// </summary> |
| 181 | + /// <param name="left">The first instance.</param> |
| 182 | + /// <param name="right">The second instance.</param> |
| 183 | + /// <returns>true if the instances are not equal; otherwise, false.</returns> |
| 184 | + public static bool operator !=(CancellationToken left, CancellationToken right) |
| 185 | + { |
| 186 | + return !left.Equals(right); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments