TaskCompletionSource.cs
Go to the documentation of this file.
1 //
2 // TaskCompletionSource.cs
3 //
4 // Authors:
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar <marek.safar@gmail.com>
7 //
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 
29 #if NET_4_0
30 using System;
32 
33 namespace System.Threading.Tasks
34 {
35  public class TaskCompletionSource<TResult>
36  {
37  readonly Task<TResult> source;
38 
40  : this (null, TaskCreationOptions.None)
41  {
42  }
43 
44  public TaskCompletionSource (object state)
45  : this (state, TaskCreationOptions.None)
46  {
47  }
48 
49  public TaskCompletionSource (TaskCreationOptions creationOptions)
50  : this (null, creationOptions)
51  {
52  }
53 
54  public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
55  {
56  if ((creationOptions & System.Threading.Tasks.Task.WorkerTaskNotSupportedOptions) != 0)
57  throw new ArgumentOutOfRangeException ("creationOptions");
58 
59  source = new Task<TResult> (TaskActionInvoker.Empty, state, CancellationToken.None, creationOptions, null);
60  source.SetupScheduler (TaskScheduler.Current);
61  }
62 
63  public void SetCanceled ()
64  {
65  if (!TrySetCanceled ())
66  ThrowInvalidException ();
67  }
68 
69  public void SetException (Exception exception)
70  {
71  if (exception == null)
72  throw new ArgumentNullException ("exception");
73 
74  SetException (new Exception[] { exception });
75  }
76 
77  public void SetException (IEnumerable<Exception> exceptions)
78  {
79  if (!TrySetException (exceptions))
80  ThrowInvalidException ();
81  }
82 
83  public void SetResult (TResult result)
84  {
85  if (!TrySetResult (result))
86  ThrowInvalidException ();
87  }
88 
89  static void ThrowInvalidException ()
90  {
91  throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
92  }
93 
94  public bool TrySetCanceled ()
95  {
96  return source.TrySetCanceled ();
97  }
98 
99  public bool TrySetException (Exception exception)
100  {
101  if (exception == null)
102  throw new ArgumentNullException ("exception");
103 
104  return TrySetException (new Exception[] { exception });
105  }
106 
107  public bool TrySetException (IEnumerable<Exception> exceptions)
108  {
109  if (exceptions == null)
110  throw new ArgumentNullException ("exceptions");
111 
112  var aggregate = new AggregateException (exceptions);
113  if (aggregate.InnerExceptions.Count == 0)
114  throw new ArgumentNullException ("exceptions");
115 
116  return source.TrySetException (aggregate);
117  }
118 
119  public bool TrySetResult (TResult result)
120  {
121  return source.TrySetResult (result);
122  }
123 
125  get {
126  return source;
127  }
128  }
129  }
130 }
131 #endif
bool TrySetException(IEnumerable< Exception > exceptions)
void SetException(IEnumerable< Exception > exceptions)
TaskCompletionSource(TaskCreationOptions creationOptions)
TaskCompletionSource(object state, TaskCreationOptions creationOptions)