mscorlib(4.0.0.0) API with additions
TaskCompletionSource.cs
4 
6 {
9  [__DynamicallyInvokable]
10  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
11  public class TaskCompletionSource<TResult>
12  {
13  private readonly Task<TResult> m_task;
14 
17  [__DynamicallyInvokable]
18  public Task<TResult> Task
19  {
20  [__DynamicallyInvokable]
21  get
22  {
23  return m_task;
24  }
25  }
26 
28  [__DynamicallyInvokable]
30  {
31  m_task = new Task<TResult>();
32  }
33 
37  [__DynamicallyInvokable]
38  public TaskCompletionSource(TaskCreationOptions creationOptions)
39  : this((object)null, creationOptions)
40  {
41  }
42 
45  [__DynamicallyInvokable]
46  public TaskCompletionSource(object state)
47  : this(state, TaskCreationOptions.None)
48  {
49  }
50 
55  [__DynamicallyInvokable]
56  public TaskCompletionSource(object state, TaskCreationOptions creationOptions)
57  {
58  m_task = new Task<TResult>(state, creationOptions);
59  }
60 
61  private void SpinUntilCompleted()
62  {
63  SpinWait spinWait = default(SpinWait);
64  while (!m_task.IsCompleted)
65  {
66  spinWait.SpinOnce();
67  }
68  }
69 
75  [__DynamicallyInvokable]
76  public bool TrySetException(Exception exception)
77  {
78  if (exception == null)
79  {
80  throw new ArgumentNullException("exception");
81  }
82  bool flag = m_task.TrySetException(exception);
83  if (!flag && !m_task.IsCompleted)
84  {
85  SpinUntilCompleted();
86  }
87  return flag;
88  }
89 
96  [__DynamicallyInvokable]
97  public bool TrySetException(IEnumerable<Exception> exceptions)
98  {
99  if (exceptions == null)
100  {
101  throw new ArgumentNullException("exceptions");
102  }
103  List<Exception> list = new List<Exception>();
104  foreach (Exception exception in exceptions)
105  {
106  if (exception == null)
107  {
108  throw new ArgumentException(Environment.GetResourceString("TaskCompletionSourceT_TrySetException_NullException"), "exceptions");
109  }
110  list.Add(exception);
111  }
112  if (list.Count == 0)
113  {
114  throw new ArgumentException(Environment.GetResourceString("TaskCompletionSourceT_TrySetException_NoExceptions"), "exceptions");
115  }
116  bool flag = m_task.TrySetException(list);
117  if (!flag && !m_task.IsCompleted)
118  {
119  SpinUntilCompleted();
120  }
121  return flag;
122  }
123 
124  internal bool TrySetException(IEnumerable<ExceptionDispatchInfo> exceptions)
125  {
126  bool flag = m_task.TrySetException(exceptions);
127  if (!flag && !m_task.IsCompleted)
128  {
129  SpinUntilCompleted();
130  }
131  return flag;
132  }
133 
139  [__DynamicallyInvokable]
140  public void SetException(Exception exception)
141  {
142  if (exception == null)
143  {
144  throw new ArgumentNullException("exception");
145  }
146  if (!TrySetException(exception))
147  {
148  throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted"));
149  }
150  }
151 
158  [__DynamicallyInvokable]
159  public void SetException(IEnumerable<Exception> exceptions)
160  {
161  if (!TrySetException(exceptions))
162  {
163  throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted"));
164  }
165  }
166 
170  [__DynamicallyInvokable]
171  public bool TrySetResult(TResult result)
172  {
173  bool flag = m_task.TrySetResult(result);
174  if (!flag && !m_task.IsCompleted)
175  {
176  SpinUntilCompleted();
177  }
178  return flag;
179  }
180 
185  [__DynamicallyInvokable]
186  public void SetResult(TResult result)
187  {
188  if (!TrySetResult(result))
189  {
190  throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted"));
191  }
192  }
193 
196  [__DynamicallyInvokable]
197  public bool TrySetCanceled()
198  {
199  return TrySetCanceled(default(CancellationToken));
200  }
201 
206  [__DynamicallyInvokable]
207  public bool TrySetCanceled(CancellationToken cancellationToken)
208  {
209  bool flag = m_task.TrySetCanceled(cancellationToken);
210  if (!flag && !m_task.IsCompleted)
211  {
212  SpinUntilCompleted();
213  }
214  return flag;
215  }
216 
219  [__DynamicallyInvokable]
220  public void SetCanceled()
221  {
222  if (!TrySetCanceled())
223  {
224  throw new InvalidOperationException(Environment.GetResourceString("TaskT_TransitionToFinal_AlreadyCompleted"));
225  }
226  }
227  }
228 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Propagates notification that operations should be canceled.
bool TrySetException(IEnumerable< Exception > exceptions)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
Provides support for spin-based waiting.
Definition: SpinWait.cs:8
void SetResult(TResult result)
Transitions the underlying T:System.Threading.Tasks.Task`1 into the F:System.Threading....
Definition: __Canon.cs:3
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
TaskCreationOptions
Specifies flags that control optional behavior for the creation and execution of tasks.
void SetCanceled()
Transitions the underlying T:System.Threading.Tasks.Task`1 into the F:System.Threading....
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
bool TrySetResult(TResult result)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
bool TrySetException(Exception exception)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
void SetException(IEnumerable< Exception > exceptions)
Transitions the underlying T:System.Threading.Tasks.Task`1 into the F:System.Threading....
bool TrySetCanceled(CancellationToken cancellationToken)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
Represents the producer side of a T:System.Threading.Tasks.Task`1 unbound to a delegate,...
The exception that is thrown when one of the arguments provided to a method is not valid.
TaskCompletionSource(TaskCreationOptions creationOptions)
Creates a T:System.Threading.Tasks.TaskCompletionSource`1 with the specified options.
TaskCompletionSource(object state)
Creates a T:System.Threading.Tasks.TaskCompletionSource`1 with the specified state.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
TaskCompletionSource()
Creates a T:System.Threading.Tasks.TaskCompletionSource`1.
void SpinOnce()
Performs a single spin.
Definition: SpinWait.cs:48
The exception that is thrown when a method call is invalid for the object's current state.
bool TrySetCanceled()
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
TaskCompletionSource(object state, TaskCreationOptions creationOptions)
Creates a T:System.Threading.Tasks.TaskCompletionSource`1 with the specified state and options.
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18
void SetException(Exception exception)
Transitions the underlying T:System.Threading.Tasks.Task`1 into the F:System.Threading....