mscorlib(4.0.0.0) API with additions
CancellationToken.cs
1 using System.Diagnostics;
4 using System.Security;
6 
7 namespace System.Threading
8 {
10  [ComVisible(false)]
11  [DebuggerDisplay("IsCancellationRequested = {IsCancellationRequested}")]
12  [__DynamicallyInvokable]
13  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
14  public struct CancellationToken
15  {
16  private CancellationTokenSource m_source;
17 
18  private static readonly Action<object> s_ActionToActionObjShunt = ActionToActionObjShunt;
19 
22  [__DynamicallyInvokable]
23  public static CancellationToken None
24  {
25  [__DynamicallyInvokable]
26  get
27  {
28  return default(CancellationToken);
29  }
30  }
31 
35  [__DynamicallyInvokable]
36  public bool IsCancellationRequested
37  {
38  [__DynamicallyInvokable]
39  get
40  {
41  if (m_source != null)
42  {
43  return m_source.IsCancellationRequested;
44  }
45  return false;
46  }
47  }
48 
52  [__DynamicallyInvokable]
53  public bool CanBeCanceled
54  {
55  [__DynamicallyInvokable]
56  get
57  {
58  if (m_source != null)
59  {
60  return m_source.CanBeCanceled;
61  }
62  return false;
63  }
64  }
65 
69  [__DynamicallyInvokable]
70  public WaitHandle WaitHandle
71  {
72  [__DynamicallyInvokable]
73  get
74  {
75  if (m_source == null)
76  {
77  InitializeDefaultSource();
78  }
79  return m_source.WaitHandle;
80  }
81  }
82 
84  {
85  m_source = source;
86  }
87 
90  [__DynamicallyInvokable]
91  public CancellationToken(bool canceled)
92  {
93  this = default(CancellationToken);
94  if (canceled)
95  {
96  m_source = CancellationTokenSource.InternalGetStaticSource(canceled);
97  }
98  }
99 
100  private static void ActionToActionObjShunt(object obj)
101  {
102  Action action = obj as Action;
103  action();
104  }
105 
112  [__DynamicallyInvokable]
114  {
115  if (callback == null)
116  {
117  throw new ArgumentNullException("callback");
118  }
119  return Register(s_ActionToActionObjShunt, callback, useSynchronizationContext: false, useExecutionContext: true);
120  }
121 
129  [__DynamicallyInvokable]
130  public CancellationTokenRegistration Register(Action callback, bool useSynchronizationContext)
131  {
132  if (callback == null)
133  {
134  throw new ArgumentNullException("callback");
135  }
136  return Register(s_ActionToActionObjShunt, callback, useSynchronizationContext, useExecutionContext: true);
137  }
138 
146  [__DynamicallyInvokable]
147  public CancellationTokenRegistration Register(Action<object> callback, object state)
148  {
149  if (callback == null)
150  {
151  throw new ArgumentNullException("callback");
152  }
153  return Register(callback, state, useSynchronizationContext: false, useExecutionContext: true);
154  }
155 
164  [__DynamicallyInvokable]
165  public CancellationTokenRegistration Register(Action<object> callback, object state, bool useSynchronizationContext)
166  {
167  return Register(callback, state, useSynchronizationContext, useExecutionContext: true);
168  }
169 
170  internal CancellationTokenRegistration InternalRegisterWithoutEC(Action<object> callback, object state)
171  {
172  return Register(callback, state, useSynchronizationContext: false, useExecutionContext: false);
173  }
174 
175  [MethodImpl(MethodImplOptions.NoInlining)]
176  [SecuritySafeCritical]
177  private CancellationTokenRegistration Register(Action<object> callback, object state, bool useSynchronizationContext, bool useExecutionContext)
178  {
179  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
180  if (callback == null)
181  {
182  throw new ArgumentNullException("callback");
183  }
184  if (!CanBeCanceled)
185  {
186  return default(CancellationTokenRegistration);
187  }
188  SynchronizationContext targetSyncContext = null;
189  ExecutionContext executionContext = null;
191  {
192  if (useSynchronizationContext)
193  {
194  targetSyncContext = SynchronizationContext.Current;
195  }
196  if (useExecutionContext)
197  {
198  executionContext = ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.OptimizeDefaultCase);
199  }
200  }
201  return m_source.InternalRegister(callback, state, targetSyncContext, executionContext);
202  }
203 
208  [__DynamicallyInvokable]
209  public bool Equals(CancellationToken other)
210  {
211  if (m_source == null && other.m_source == null)
212  {
213  return true;
214  }
215  if (m_source == null)
216  {
217  return other.m_source == CancellationTokenSource.InternalGetStaticSource(set: false);
218  }
219  if (other.m_source == null)
220  {
221  return m_source == CancellationTokenSource.InternalGetStaticSource(set: false);
222  }
223  return m_source == other.m_source;
224  }
225 
231  [__DynamicallyInvokable]
232  public override bool Equals(object other)
233  {
234  if (other is CancellationToken)
235  {
236  return Equals((CancellationToken)other);
237  }
238  return false;
239  }
240 
243  [__DynamicallyInvokable]
244  public override int GetHashCode()
245  {
246  if (m_source == null)
247  {
248  return CancellationTokenSource.InternalGetStaticSource(set: false).GetHashCode();
249  }
250  return m_source.GetHashCode();
251  }
252 
259  [__DynamicallyInvokable]
260  public static bool operator ==(CancellationToken left, CancellationToken right)
261  {
262  return left.Equals(right);
263  }
264 
271  [__DynamicallyInvokable]
272  public static bool operator !=(CancellationToken left, CancellationToken right)
273  {
274  return !left.Equals(right);
275  }
276 
280  [__DynamicallyInvokable]
282  {
284  {
285  ThrowOperationCanceledException();
286  }
287  }
288 
289  internal void ThrowIfSourceDisposed()
290  {
291  if (m_source != null && m_source.IsDisposed)
292  {
293  ThrowObjectDisposedException();
294  }
295  }
296 
297  private void ThrowOperationCanceledException()
298  {
299  throw new OperationCanceledException(Environment.GetResourceString("OperationCanceled"), this);
300  }
301 
302  private static void ThrowObjectDisposedException()
303  {
304  throw new ObjectDisposedException(null, Environment.GetResourceString("CancellationToken_SourceDisposed"));
305  }
306 
307  private void InitializeDefaultSource()
308  {
309  m_source = CancellationTokenSource.InternalGetStaticSource(set: false);
310  }
311  }
312 }
CancellationTokenRegistration Register(Action callback, bool useSynchronizationContext)
Registers a delegate that will be called when this T:System.Threading.CancellationToken is canceled.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Propagates notification that operations should be canceled.
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
Definition: WaitHandle.cs:15
void ThrowIfCancellationRequested()
Throws a T:System.OperationCanceledException if this token has had cancellation requested.
Definition: __Canon.cs:3
Represents a callback delegate that has been registered with a T:System.Threading....
override bool Equals(object other)
Determines whether the current T:System.Threading.CancellationToken instance is equal to the specifie...
bool CanBeCanceled
Gets whether this token is capable of being in the canceled state.
delegate void Action()
Encapsulates a method that has no parameters and does not return a value.
bool IsCancellationRequested
Gets whether cancellation has been requested for this token.
override int GetHashCode()
Serves as a hash function for a T:System.Threading.CancellationToken.
CancellationTokenRegistration Register(Action< object > callback, object state, bool useSynchronizationContext)
Registers a delegate that will be called when this T:System.Threading.CancellationToken is canceled.
SecurityAction
Specifies the security actions that can be performed using declarative security.
CancellationToken(bool canceled)
Initializes the T:System.Threading.CancellationToken.
MethodImplOptions
Defines the details of how a method is implemented.
static CancellationToken None
Returns an empty T:System.Threading.CancellationToken value.
static bool operator==(CancellationToken left, CancellationToken right)
Determines whether two T:System.Threading.CancellationToken instances are equal.
CancellationTokenRegistration Register(Action< object > callback, object state)
Registers a delegate that will be called when this T:System.Threading.CancellationToken is canceled.
Signals to a T:System.Threading.CancellationToken that it should be canceled.
bool Equals(CancellationToken other)
Determines whether the current T:System.Threading.CancellationToken instance is equal to the specifie...
static bool operator !=(CancellationToken left, CancellationToken right)
Determines whether two T:System.Threading.CancellationToken instances are not equal.
bool IsCancellationRequested
Gets whether cancellation has been requested for this T:System.Threading.CancellationTokenSource.
CancellationTokenRegistration Register(Action callback)
Registers a delegate that will be called when this T:System.Threading.CancellationToken is canceled.