mscorlib(4.0.0.0) API with additions
YieldAwaitable.cs
3 using System.Security;
5 using System.Threading;
7 
9 {
11  [StructLayout(LayoutKind.Sequential, Size = 1)]
12  [__DynamicallyInvokable]
13  public struct YieldAwaitable
14  {
16  [StructLayout(LayoutKind.Sequential, Size = 1)]
17  [__DynamicallyInvokable]
18  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
20  {
21  private static readonly WaitCallback s_waitCallbackRunAction = RunAction;
22 
23  private static readonly SendOrPostCallback s_sendOrPostCallbackRunAction = RunAction;
24 
27  [__DynamicallyInvokable]
28  public bool IsCompleted
29  {
30  [__DynamicallyInvokable]
31  get
32  {
33  return false;
34  }
35  }
36 
41  [SecuritySafeCritical]
42  [__DynamicallyInvokable]
43  public void OnCompleted(Action continuation)
44  {
45  QueueContinuation(continuation, flowContext: true);
46  }
47 
51  [SecurityCritical]
52  [__DynamicallyInvokable]
53  public void UnsafeOnCompleted(Action continuation)
54  {
55  QueueContinuation(continuation, flowContext: false);
56  }
57 
58  [SecurityCritical]
59  private static void QueueContinuation(Action continuation, bool flowContext)
60  {
61  if (continuation == null)
62  {
63  throw new ArgumentNullException("continuation");
64  }
65  if (TplEtwProvider.Log.IsEnabled())
66  {
67  continuation = OutputCorrelationEtwEvent(continuation);
68  }
69  SynchronizationContext currentNoFlow = SynchronizationContext.CurrentNoFlow;
70  if (currentNoFlow != null && currentNoFlow.GetType() != typeof(SynchronizationContext))
71  {
72  currentNoFlow.Post(s_sendOrPostCallbackRunAction, continuation);
73  return;
74  }
76  if (current == TaskScheduler.Default)
77  {
78  if (flowContext)
79  {
80  ThreadPool.QueueUserWorkItem(s_waitCallbackRunAction, continuation);
81  }
82  else
83  {
84  ThreadPool.UnsafeQueueUserWorkItem(s_waitCallbackRunAction, continuation);
85  }
86  }
87  else
88  {
89  Task.Factory.StartNew(continuation, default(CancellationToken), TaskCreationOptions.PreferFairness, current);
90  }
91  }
92 
93  private static Action OutputCorrelationEtwEvent(Action continuation)
94  {
95  int continuationId = Task.NewId();
96  Task internalCurrent = Task.InternalCurrent;
97  TplEtwProvider.Log.AwaitTaskContinuationScheduled(TaskScheduler.Current.Id, internalCurrent?.Id ?? 0, continuationId);
98  return AsyncMethodBuilderCore.CreateContinuationWrapper(continuation, delegate
99  {
100  TplEtwProvider log = TplEtwProvider.Log;
101  log.TaskWaitContinuationStarted(continuationId);
102  Guid oldActivityThatWillContinue = default(Guid);
103  if (log.TasksSetActivityIds)
104  {
105  EventSource.SetCurrentThreadActivityId(TplEtwProvider.CreateGuidForTaskID(continuationId), out oldActivityThatWillContinue);
106  }
107  continuation();
108  if (log.TasksSetActivityIds)
109  {
110  EventSource.SetCurrentThreadActivityId(oldActivityThatWillContinue);
111  }
112  log.TaskWaitContinuationComplete(continuationId);
113  });
114  }
115 
116  private static void RunAction(object state)
117  {
118  ((Action)state)();
119  }
120 
122  [__DynamicallyInvokable]
123  public void GetResult()
124  {
125  }
126  }
127 
130  [__DynamicallyInvokable]
132  {
133  return default(YieldAwaiter);
134  }
135  }
136 }
static new TaskFactory< TResult > Factory
Provides access to factory methods for creating and configuring T:System.Threading....
Definition: Task.cs:75
virtual void Post(SendOrPostCallback d, object state)
When overridden in a derived class, dispatches an asynchronous message to a synchronization context.
YieldAwaiter GetAwaiter()
Retrieves a T:System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter object for this instance of...
static TaskScheduler?? Current
Gets the T:System.Threading.Tasks.TaskScheduler associated with the currently executing task.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents an operation that schedules continuations when it completes.
Propagates notification that operations should be canceled.
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
LayoutKind
Controls the layout of an object when exported to unmanaged code.
Definition: LayoutKind.cs:7
Represents an object that handles the low-level work of queuing tasks onto threads.
Definition: __Canon.cs:3
static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
Queues the specified delegate to the thread pool, but does not propagate the calling stack to the wor...
Definition: ThreadPool.cs:312
delegate void Action()
Encapsulates a method that has no parameters and does not return a value.
TaskCreationOptions
Specifies flags that control optional behavior for the creation and execution of tasks.
void UnsafeOnCompleted(Action continuation)
Posts the continuation back to the current context.
Provides the context for waiting when asynchronously switching into a target environment.
static TaskScheduler Default
Gets the default T:System.Threading.Tasks.TaskScheduler instance that is provided by the ....
SecurityAction
Specifies the security actions that can be performed using declarative security.
int Id
Gets the unique ID for this T:System.Threading.Tasks.TaskScheduler.
bool IsCompleted
Gets a value that indicates whether a yield is not required.
static void SetCurrentThreadActivityId(Guid activityId)
[Supported in the .NET Framework 4.5.1 and later versions] Sets the activity ID on the current thread...
Definition: EventSource.cs:649
Provides the ability to create events for event tracing for Windows (ETW).
Definition: EventSource.cs:21
Provides the basic functionality for propagating a synchronization context in various synchronization...
void OnCompleted(Action continuation)
Sets the continuation to invoke.
Creates or manipulates threads other than its own, which might be harmful to the host.
delegate void WaitCallback(object state)
Represents a callback method to be executed by a thread pool thread.
Represents an awaiter that schedules continuations when an await operation completes.
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition: ThreadPool.cs:14
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18
Provides an awaiter for switching into a target environment.