2 // TaskCompletionSource.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Collections.Generic;
31 namespace System.Threading.Tasks
33 public class TaskCompletionSource<TResult>
37 public TaskCompletionSource ()
39 source = new Task<TResult> (null);
40 source.SetupScheduler (TaskScheduler.Current);
43 public TaskCompletionSource (object state)
45 source = new Task<TResult> (null, state);
46 source.SetupScheduler (TaskScheduler.Current);
49 public TaskCompletionSource (TaskCreationOptions creationOptions)
51 source = new Task<TResult> (null, creationOptions);
52 source.SetupScheduler (TaskScheduler.Current);
55 public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
57 source = new Task<TResult> (null, state, creationOptions);
58 source.SetupScheduler (TaskScheduler.Current);
61 public void SetCanceled ()
63 if (!ApplyOperation (source.CancelReal))
64 ThrowInvalidException ();
67 public void SetException (Exception exception)
69 SetException (new Exception[] { exception });
72 public void SetException (IEnumerable<Exception> exceptions)
74 if (!ApplyOperation (() => source.HandleGenericException (new AggregateException (exceptions))))
75 ThrowInvalidException ();
78 public void SetResult (TResult result)
80 if (!ApplyOperation (() => source.Result = result))
81 ThrowInvalidException ();
84 void ThrowInvalidException ()
86 throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
89 public bool TrySetCanceled ()
91 return ApplyOperation (source.CancelReal);
94 public bool TrySetException (Exception exception)
96 return TrySetException (new Exception[] { exception });
99 public bool TrySetException (IEnumerable<Exception> exceptions)
101 return ApplyOperation (() => source.HandleGenericException (new AggregateException (exceptions)));
104 public bool TrySetResult (TResult result)
106 return ApplyOperation (() => source.Result = result);
109 bool ApplyOperation (Action action)
111 if (CheckInvalidState ())
114 source.Status = TaskStatus.Running;
124 bool CheckInvalidState ()
126 return source.Status == TaskStatus.RanToCompletion ||
127 source.Status == TaskStatus.Faulted ||
128 source.Status == TaskStatus.Canceled;
132 public Task<TResult> Task {