2 // TaskCompletionSource.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar <marek.safar@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
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
31 using System.Collections.Generic;
33 namespace System.Threading.Tasks
35 public class TaskCompletionSource<TResult>
37 readonly Task<TResult> source;
39 public TaskCompletionSource ()
40 : this (null, TaskCreationOptions.None)
44 public TaskCompletionSource (object state)
45 : this (state, TaskCreationOptions.None)
49 public TaskCompletionSource (TaskCreationOptions creationOptions)
50 : this (null, creationOptions)
54 public TaskCompletionSource (object state, TaskCreationOptions creationOptions)
56 if ((creationOptions & System.Threading.Tasks.Task.WorkerTaskNotSupportedOptions) != 0)
57 throw new ArgumentOutOfRangeException ("creationOptions");
59 source = new Task<TResult> (TaskActionInvoker.Empty, state, CancellationToken.None, creationOptions, null);
60 source.SetupScheduler (TaskScheduler.Current);
63 public void SetCanceled ()
65 if (!TrySetCanceled ())
66 ThrowInvalidException ();
69 public void SetException (Exception exception)
71 if (exception == null)
72 throw new ArgumentNullException ("exception");
74 SetException (new Exception[] { exception });
77 public void SetException (IEnumerable<Exception> exceptions)
79 if (!TrySetException (exceptions))
80 ThrowInvalidException ();
83 public void SetResult (TResult result)
85 if (!TrySetResult (result))
86 ThrowInvalidException ();
89 static void ThrowInvalidException ()
91 throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
94 public bool TrySetCanceled ()
96 return source.TrySetCanceled ();
99 public bool TrySetException (Exception exception)
101 if (exception == null)
102 throw new ArgumentNullException ("exception");
104 return TrySetException (new Exception[] { exception });
107 public bool TrySetException (IEnumerable<Exception> exceptions)
109 if (exceptions == null)
110 throw new ArgumentNullException ("exceptions");
112 var aggregate = new AggregateException (exceptions);
113 if (aggregate.InnerExceptions.Count == 0)
114 throw new ArgumentNullException ("exceptions");
116 return source.TrySetException (aggregate, false, false);
119 public bool TrySetResult (TResult result)
121 return source.TrySetResult (result);
124 public Task<TResult> Task {