mscorlib(4.0.0.0) API with additions
CancellationTokenRegistration.cs
3 
4 namespace System.Threading
5 {
7  [__DynamicallyInvokable]
8  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
9  public struct CancellationTokenRegistration : IEquatable<CancellationTokenRegistration>, IDisposable
10  {
11  private readonly CancellationCallbackInfo m_callbackInfo;
12 
13  private readonly SparselyPopulatedArrayAddInfo<CancellationCallbackInfo> m_registrationInfo;
14 
15  internal CancellationTokenRegistration(CancellationCallbackInfo callbackInfo, SparselyPopulatedArrayAddInfo<CancellationCallbackInfo> registrationInfo)
16  {
17  m_callbackInfo = callbackInfo;
18  m_registrationInfo = registrationInfo;
19  }
20 
21  [FriendAccessAllowed]
22  internal bool TryDeregister()
23  {
24  if (m_registrationInfo.Source == null)
25  {
26  return false;
27  }
28  CancellationCallbackInfo cancellationCallbackInfo = m_registrationInfo.Source.SafeAtomicRemove(m_registrationInfo.Index, m_callbackInfo);
29  if (cancellationCallbackInfo != m_callbackInfo)
30  {
31  return false;
32  }
33  return true;
34  }
35 
37  [__DynamicallyInvokable]
38  public void Dispose()
39  {
40  bool flag = TryDeregister();
41  CancellationCallbackInfo callbackInfo = m_callbackInfo;
42  if (callbackInfo != null)
43  {
44  CancellationTokenSource cancellationTokenSource = callbackInfo.CancellationTokenSource;
45  if (cancellationTokenSource.IsCancellationRequested && !cancellationTokenSource.IsCancellationCompleted && !flag && cancellationTokenSource.ThreadIDExecutingCallbacks != Thread.CurrentThread.ManagedThreadId)
46  {
47  cancellationTokenSource.WaitForCallbackToComplete(m_callbackInfo);
48  }
49  }
50  }
51 
56  [__DynamicallyInvokable]
58  {
59  return left.Equals(right);
60  }
61 
66  [__DynamicallyInvokable]
68  {
69  return !left.Equals(right);
70  }
71 
75  [__DynamicallyInvokable]
76  public override bool Equals(object obj)
77  {
79  {
81  }
82  return false;
83  }
84 
88  [__DynamicallyInvokable]
90  {
91  if (m_callbackInfo == other.m_callbackInfo && m_registrationInfo.Source == other.m_registrationInfo.Source)
92  {
93  return m_registrationInfo.Index == other.m_registrationInfo.Index;
94  }
95  return false;
96  }
97 
100  [__DynamicallyInvokable]
101  public override int GetHashCode()
102  {
103  if (m_registrationInfo.Source != null)
104  {
105  return m_registrationInfo.Source.GetHashCode() ^ m_registrationInfo.Index.GetHashCode();
106  }
107  return m_registrationInfo.Index.GetHashCode();
108  }
109  }
110 }
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
static bool operator==(CancellationTokenRegistration left, CancellationTokenRegistration right)
Determines whether two T:System.Threading.CancellationTokenRegistration instances are equal.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
override int GetHashCode()
Serves as a hash function for a T:System.Threading.CancellationTokenRegistration.
Definition: __Canon.cs:3
Represents a callback delegate that has been registered with a T:System.Threading....
SecurityAction
Specifies the security actions that can be performed using declarative security.
static bool operator !=(CancellationTokenRegistration left, CancellationTokenRegistration right)
Determines whether two T:System.Threading.CancellationTokenRegistration instances are not equal.
override bool Equals(object obj)
Determines whether the current T:System.Threading.CancellationTokenRegistration instance is equal to ...
CancellationTokenSource()
Initializes a new instance of the T:System.Threading.CancellationTokenSource class.
int ManagedThreadId
Gets a unique identifier for the current managed thread.
Definition: Thread.cs:55
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
Signals to a T:System.Threading.CancellationToken that it should be canceled.
bool Equals(CancellationTokenRegistration other)
Determines whether the current T:System.Threading.CancellationTokenRegistration instance is equal to ...
bool IsCancellationRequested
Gets whether cancellation has been requested for this T:System.Threading.CancellationTokenSource.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.CancellationTokenRegist...
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18