mscorlib(4.0.0.0) API with additions
AsyncFlowControl.cs
1 using System.Security;
2 
3 namespace System.Threading
4 {
6  public struct AsyncFlowControl : IDisposable
7  {
8  private bool useEC;
9 
10  private ExecutionContext _ec;
11 
12  private SecurityContext _sc;
13 
14  private Thread _thread;
15 
16  [SecurityCritical]
17  internal void Setup(SecurityContextDisableFlow flags)
18  {
19  useEC = false;
20  Thread currentThread = Thread.CurrentThread;
21  _sc = currentThread.GetMutableExecutionContext().SecurityContext;
22  _sc._disableFlow = flags;
23  _thread = currentThread;
24  }
25 
26  [SecurityCritical]
27  internal void Setup()
28  {
29  useEC = true;
30  Thread currentThread = Thread.CurrentThread;
31  _ec = currentThread.GetMutableExecutionContext();
32  _ec.isFlowSuppressed = true;
33  _thread = currentThread;
34  }
35 
38  public void Dispose()
39  {
40  Undo();
41  }
42 
45  [SecuritySafeCritical]
46  public void Undo()
47  {
48  if (_thread == null)
49  {
50  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseAFCMultiple"));
51  }
52  if (_thread != Thread.CurrentThread)
53  {
54  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseAFCOtherThread"));
55  }
56  if (useEC)
57  {
58  if (Thread.CurrentThread.GetMutableExecutionContext() != _ec)
59  {
60  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_AsyncFlowCtrlCtxMismatch"));
61  }
63  }
64  else
65  {
66  if (!Thread.CurrentThread.GetExecutionContextReader().SecurityContext.IsSame(_sc))
67  {
68  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_AsyncFlowCtrlCtxMismatch"));
69  }
71  }
72  _thread = null;
73  }
74 
77  public override int GetHashCode()
78  {
79  if (_thread != null)
80  {
81  return _thread.GetHashCode();
82  }
83  return ToString().GetHashCode();
84  }
85 
90  public override bool Equals(object obj)
91  {
92  if (obj is AsyncFlowControl)
93  {
94  return Equals((AsyncFlowControl)obj);
95  }
96  return false;
97  }
98 
103  public bool Equals(AsyncFlowControl obj)
104  {
105  if (obj.useEC == useEC && obj._ec == _ec && obj._sc == _sc)
106  {
107  return obj._thread == _thread;
108  }
109  return false;
110  }
111 
118  {
119  return a.Equals(b);
120  }
121 
128  {
129  return !(a == b);
130  }
131  }
132 }
void Undo()
Restores the flow of the execution context between threads.
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
static void RestoreFlow()
Restores the flow of the security context across asynchronous threads.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.AsyncFlowControl class.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Definition: __Canon.cs:3
Encapsulates and propagates all security-related data for execution contexts transferred across threa...
override int GetHashCode()
Gets a hash code for the current T:System.Threading.AsyncFlowControl structure.
Provides the functionality to restore the migration, or flow, of the execution context between thread...
override bool Equals(object obj)
Determines whether the specified object is equal to the current T:System.Threading....
override int GetHashCode()
Returns a hash code for the current thread.
Definition: Thread.cs:460
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Manages the execution context for the current thread. This class cannot be inherited.
bool Equals(AsyncFlowControl obj)
Determines whether the specified T:System.Threading.AsyncFlowControl structure is equal to the curren...
static void RestoreFlow()
Restores the flow of the execution context across asynchronous threads.
static bool operator !=(AsyncFlowControl a, AsyncFlowControl b)
Compares two T:System.Threading.AsyncFlowControl structures to determine whether they are not equal.
The exception that is thrown when a method call is invalid for the object's current state.
static bool operator==(AsyncFlowControl a, AsyncFlowControl b)
Compares two T:System.Threading.AsyncFlowControl structures to determine whether they are equal.
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18