2 // CancellationToken.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Threading;
33 using System.Diagnostics;
35 namespace System.Threading
37 [DebuggerDisplay ("IsCancellationRequested = {IsCancellationRequested}")]
38 public struct CancellationToken
40 readonly CancellationTokenSource source;
42 public CancellationToken (bool canceled)
43 : this (canceled ? CancellationTokenSource.CanceledSource : null)
47 internal CancellationToken (CancellationTokenSource source)
52 public static CancellationToken None {
54 // simply return new struct value, it's the fastest option
55 // and we don't have to bother with reseting source
56 return new CancellationToken ();
60 public CancellationTokenRegistration Register (Action callback)
62 return Register (callback, false);
65 public CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext)
68 throw new ArgumentNullException ("callback");
70 return Source.Register (callback, useSynchronizationContext);
73 public CancellationTokenRegistration Register (Action<object> callback, object state)
75 return Register (callback, state, false);
78 public CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext)
81 throw new ArgumentNullException ("callback");
83 return Register (() => callback (state), useSynchronizationContext);
86 public void ThrowIfCancellationRequested ()
88 if (source != null && source.IsCancellationRequested)
89 throw new OperationCanceledException (this);
92 public bool Equals (CancellationToken other)
94 return this.Source == other.Source;
97 public override bool Equals (object other)
99 return (other is CancellationToken) ? Equals ((CancellationToken)other) : false;
102 public override int GetHashCode ()
104 return Source.GetHashCode ();
107 public static bool operator == (CancellationToken left, CancellationToken right)
109 return left.Equals (right);
112 public static bool operator != (CancellationToken left, CancellationToken right)
114 return !left.Equals (right);
117 public bool CanBeCanceled {
119 return source != null;
123 public bool IsCancellationRequested {
125 return Source.IsCancellationRequested;
129 public WaitHandle WaitHandle {
131 return Source.WaitHandle;
135 CancellationTokenSource Source {
137 return source ?? CancellationTokenSource.NoneSource;