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;
30 #if NET_4_0 || BOOTSTRAP_NET_4_0
31 namespace System.Threading
33 public struct CancellationToken
35 public CancellationToken (bool canceled)
38 // dummy, this is actually set by CancellationTokenSource when the token is created
42 public static CancellationToken None {
44 return CancellationTokenSource.NoneSource.Token;
48 public CancellationTokenRegistration Register (Action callback)
50 return Register (callback, false);
53 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
56 throw new ArgumentNullException ("callback");
58 return Source.Register (callback, useSynchronizationContext);
61 public CancellationTokenRegistration Register (Action<object> callback, object state)
63 return Register (callback, state, false);
66 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
69 throw new ArgumentNullException ("callback");
71 return Register (() => callback (state), useSynchronizationContext);
74 public void ThrowIfCancellationRequested ()
76 Source.ThrowIfCancellationRequested ();
79 public bool Equals (CancellationToken other)
81 return this.Source == other.Source;
84 public override bool Equals (object obj)
86 return (obj is CancellationToken) ? Equals ((CancellationToken)obj) : false;
89 public override int GetHashCode ()
91 return Source.GetHashCode ();
94 public static bool operator == (CancellationToken lhs, CancellationToken rhs)
96 return lhs.Equals (rhs);
99 public static bool operator != (CancellationToken lhs, CancellationToken rhs)
101 return !lhs.Equals (rhs);
104 public bool CanBeCanceled {
110 public bool IsCancellationRequested {
112 return Source.IsCancellationRequested;
116 public WaitHandle WaitHandle {
118 return Source.WaitHandle;
122 internal CancellationTokenSource Source {