2 // CancellationToken.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 using System.Threading;
31 namespace System.Threading
33 [System.Diagnostics.DebuggerDisplay ("IsCancellationRequested = {IsCancellationRequested}")]
34 public struct CancellationToken
38 CancellationTokenSource source;
40 public CancellationToken (bool canceled)
44 canBeCanceled = canceled;
45 // This is correctly set later if token originates from a Source
46 source = canceled ? CancellationTokenSource.CanceledSource : CancellationTokenSource.NoneSource;
49 public static CancellationToken None {
51 return CancellationTokenSource.NoneSource.Token;
55 public CancellationTokenRegistration Register (Action callback)
57 return Register (callback, false);
60 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
63 throw new ArgumentNullException ("callback");
65 return Source.Register (callback, useSynchronizationContext);
68 public CancellationTokenRegistration Register (Action<object> callback, object state)
70 return Register (callback, state, false);
73 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
76 throw new ArgumentNullException ("callback");
78 return Register (() => callback (state), useSynchronizationContext);
81 public void ThrowIfCancellationRequested ()
83 if (initialized && Source.IsCancellationRequested)
84 throw new OperationCanceledException (this);
87 public bool Equals (CancellationToken other)
89 return this.Source == other.Source;
92 public override bool Equals (object other)
94 return (other is CancellationToken) ? Equals ((CancellationToken)other) : false;
97 public override int GetHashCode ()
99 return Source.GetHashCode ();
102 public static bool operator == (CancellationToken left, CancellationToken right)
104 return left.Equals (right);
107 public static bool operator != (CancellationToken left, CancellationToken right)
109 return !left.Equals (right);
112 public bool CanBeCanceled {
114 return canBeCanceled;
118 public bool IsCancellationRequested {
120 return initialized && Source.IsCancellationRequested;
124 public WaitHandle WaitHandle {
126 return Source.WaitHandle;
130 internal CancellationTokenSource Source {
133 CorrectlyInitialize ();
141 void CorrectlyInitialize ()
143 Source = CancellationTokenSource.NoneSource;