mscorlib(4.0.0.0) API with additions
SynchronizationContext.cs
4 using System.Security;
6 
7 namespace System.Threading
8 {
10  [__DynamicallyInvokable]
11  [SecurityPermission(SecurityAction.InheritanceDemand, Flags = (SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy))]
13  {
14  private delegate int WaitDelegate(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout);
15 
16  private SynchronizationContextProperties _props;
17 
18  private static Type s_cachedPreparedType1;
19 
20  private static Type s_cachedPreparedType2;
21 
22  private static Type s_cachedPreparedType3;
23 
24  private static Type s_cachedPreparedType4;
25 
26  private static Type s_cachedPreparedType5;
27 
28  [SecurityCritical]
29  private static WinRTSynchronizationContextFactoryBase s_winRTContextFactory;
30 
33  [__DynamicallyInvokable]
34  public static SynchronizationContext Current
35  {
36  [__DynamicallyInvokable]
37  get
38  {
39  return Thread.CurrentThread.GetExecutionContextReader().SynchronizationContext ?? GetThreadLocalContext();
40  }
41  }
42 
43  internal static SynchronizationContext CurrentNoFlow
44  {
45  [FriendAccessAllowed]
46  get
47  {
48  return Thread.CurrentThread.GetExecutionContextReader().SynchronizationContextNoFlow ?? GetThreadLocalContext();
49  }
50  }
51 
53  [__DynamicallyInvokable]
55  {
56  }
57 
59  [SecuritySafeCritical]
60  protected void SetWaitNotificationRequired()
61  {
62  Type type = GetType();
63  if (s_cachedPreparedType1 != type && s_cachedPreparedType2 != type && s_cachedPreparedType3 != type && s_cachedPreparedType4 != type && s_cachedPreparedType5 != type)
64  {
65  RuntimeHelpers.PrepareDelegate(new WaitDelegate(Wait));
66  if (s_cachedPreparedType1 == null)
67  {
68  s_cachedPreparedType1 = type;
69  }
70  else if (s_cachedPreparedType2 == null)
71  {
72  s_cachedPreparedType2 = type;
73  }
74  else if (s_cachedPreparedType3 == null)
75  {
76  s_cachedPreparedType3 = type;
77  }
78  else if (s_cachedPreparedType4 == null)
79  {
80  s_cachedPreparedType4 = type;
81  }
82  else if (s_cachedPreparedType5 == null)
83  {
84  s_cachedPreparedType5 = type;
85  }
86  }
87  _props |= SynchronizationContextProperties.RequireWaitNotification;
88  }
89 
94  {
95  return (_props & SynchronizationContextProperties.RequireWaitNotification) != SynchronizationContextProperties.None;
96  }
97 
102  [__DynamicallyInvokable]
103  public virtual void Send(SendOrPostCallback d, object state)
104  {
105  d(state);
106  }
107 
111  [__DynamicallyInvokable]
112  public virtual void Post(SendOrPostCallback d, object state)
113  {
114  ThreadPool.QueueUserWorkItem(d.Invoke, state);
115  }
116 
118  [__DynamicallyInvokable]
119  public virtual void OperationStarted()
120  {
121  }
122 
124  [__DynamicallyInvokable]
125  public virtual void OperationCompleted()
126  {
127  }
128 
137  [SecurityCritical]
138  [CLSCompliant(false)]
139  [PrePrepareMethod]
140  public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
141  {
142  if (waitHandles == null)
143  {
144  throw new ArgumentNullException("waitHandles");
145  }
146  return WaitHelper(waitHandles, waitAll, millisecondsTimeout);
147  }
148 
155  [MethodImpl(MethodImplOptions.InternalCall)]
156  [SecurityCritical]
157  [CLSCompliant(false)]
158  [PrePrepareMethod]
159  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
160  protected static extern int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout);
161 
164  [SecurityCritical]
165  [__DynamicallyInvokable]
166  public static void SetSynchronizationContext(SynchronizationContext syncContext)
167  {
168  ExecutionContext mutableExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();
169  mutableExecutionContext.SynchronizationContext = syncContext;
170  mutableExecutionContext.SynchronizationContextNoFlow = syncContext;
171  }
172 
173  private static SynchronizationContext GetThreadLocalContext()
174  {
175  SynchronizationContext synchronizationContext = null;
176  if (synchronizationContext == null && Environment.IsWinRTSupported)
177  {
178  synchronizationContext = GetWinRTContext();
179  }
180  return synchronizationContext;
181  }
182 
183  [SecuritySafeCritical]
184  private static SynchronizationContext GetWinRTContext()
185  {
186  if (!AppDomain.IsAppXModel())
187  {
188  return null;
189  }
190  object winRTDispatcherForCurrentThread = GetWinRTDispatcherForCurrentThread();
191  if (winRTDispatcherForCurrentThread != null)
192  {
193  return GetWinRTSynchronizationContextFactory().Create(winRTDispatcherForCurrentThread);
194  }
195  return null;
196  }
197 
198  [SecurityCritical]
199  private static WinRTSynchronizationContextFactoryBase GetWinRTSynchronizationContextFactory()
200  {
201  WinRTSynchronizationContextFactoryBase winRTSynchronizationContextFactoryBase = s_winRTContextFactory;
202  if (winRTSynchronizationContextFactoryBase == null)
203  {
204  Type type = Type.GetType("System.Threading.WinRTSynchronizationContextFactory, System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", throwOnError: true);
205  winRTSynchronizationContextFactoryBase = (s_winRTContextFactory = (WinRTSynchronizationContextFactoryBase)Activator.CreateInstance(type, nonPublic: true));
206  }
207  return winRTSynchronizationContextFactoryBase;
208  }
209 
210  [DllImport("QCall", CharSet = CharSet.Unicode)]
211  [SecurityCritical]
212  [SuppressUnmanagedCodeSecurity]
213  [return: MarshalAs(UnmanagedType.Interface)]
214  private static extern object GetWinRTDispatcherForCurrentThread();
215 
218  [__DynamicallyInvokable]
220  {
221  return new SynchronizationContext();
222  }
223 
224  [SecurityCritical]
225  private static int InvokeWaitMethodHelper(SynchronizationContext syncContext, IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
226  {
227  return syncContext.Wait(waitHandles, waitAll, millisecondsTimeout);
228  }
229  }
230 }
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
virtual void Post(SendOrPostCallback d, object state)
When overridden in a derived class, dispatches an asynchronous message to a synchronization context.
virtual void Send(SendOrPostCallback d, object state)
When overridden in a derived class, dispatches a synchronous message to a synchronization context.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
static SynchronizationContext?? Current
Gets the synchronization context for the current thread.
delegate void SendOrPostCallback(object state)
Represents a method to be called when a message is to be dispatched to a synchronization context.
static bool QueueUserWorkItem(WaitCallback callBack, object state)
Queues a method for execution, and specifies an object containing data to be used by the method....
Definition: ThreadPool.cs:278
void SetWaitNotificationRequired()
Sets notification that wait notification is required and prepares the callback method so it can be ca...
Definition: __Canon.cs:3
virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
Waits for any or all the elements in the specified array to receive a signal.
static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
Helper function that waits for any or all the elements in the specified array to receive a signal.
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
SecurityAction
Specifies the security actions that can be performed using declarative security.
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.
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
bool IsWaitNotificationRequired()
Determines if wait notification is required.
SynchronizationContext()
Creates a new instance of the T:System.Threading.SynchronizationContext class.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Provides the basic functionality for propagating a synchronization context in various synchronization...
virtual void OperationCompleted()
When overridden in a derived class, responds to the notification that an operation has completed.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
virtual SynchronizationContext CreateCopy()
When overridden in a derived class, creates a copy of the synchronization context.
static void SetSynchronizationContext(SynchronizationContext syncContext)
Sets the current synchronization context.
virtual void OperationStarted()
When overridden in a derived class, responds to the notification that an operation has started.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition: ThreadPool.cs:14
Provides a set of static methods and properties that provide support for compilers....
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18
static void PrepareDelegate(Delegate d)
Indicates that the specified delegate should be prepared for inclusion in a constrained execution reg...