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 if (Source.IsCancellationRequested)
77 throw new OperationCanceledException (this);
80 public bool Equals (CancellationToken other)
82 return this.Source == other.Source;
85 public override bool Equals (object obj)
87 return (obj is CancellationToken) ? Equals ((CancellationToken)obj) : false;
90 public override int GetHashCode ()
92 return Source.GetHashCode ();
95 public static bool operator == (CancellationToken lhs, CancellationToken rhs)
97 return lhs.Equals (rhs);
100 public static bool operator != (CancellationToken lhs, CancellationToken rhs)
102 return !lhs.Equals (rhs);
105 public bool CanBeCanceled {
111 public bool IsCancellationRequested {
113 return Source.IsCancellationRequested;
117 public WaitHandle WaitHandle {
119 return Source.WaitHandle;
123 internal CancellationTokenSource Source {