Merge pull request #121 from LogosBible/processfixes
[mono.git] / mcs / class / corlib / System.Threading.Tasks / Task_T.cs
1 //
2 // Task_T.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 #if NET_4_0 || MOBILE
27 using System;
28
29 namespace System.Threading.Tasks
30 {
31         [System.Diagnostics.DebuggerDisplay ("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}")]
32         [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.SystemThreadingTasks_FutureDebugView`1")]
33         public class Task<TResult>: Task
34         {
35                 TResult value;
36                 static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
37                 
38                 Func<object, TResult> function;
39                 object state;
40                 
41                 [System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]
42                 public TResult Result {
43                         get {
44                                 if (function != null)
45                                         Wait ();
46                                 else if (Exception != null)
47                                         throw Exception;
48                                 return value;
49                         }
50                         internal set {
51                                 this.value = value;
52                         }
53                 }
54                 
55                 public static new TaskFactory<TResult> Factory {
56                         get {
57                                 return factory;
58                         }
59                 }
60                 
61                 public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
62                 {
63                         
64                 }
65                 
66                 public Task (Func<TResult> function, CancellationToken cancellationToken)
67                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
68                 {
69                         
70                 }
71                 
72                 public Task (Func<TResult> function, TaskCreationOptions creationOptions)
73                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
74                 {
75                         
76                 }
77                 
78                 public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
79                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
80                 {
81                         
82                 }
83                 
84                 public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
85                 {
86                         
87                 }
88                 
89                 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
90                         : this (function, state, cancellationToken, TaskCreationOptions.None)
91                 {
92                         
93                 }
94                 
95                 public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
96                         : this (function, state, CancellationToken.None, creationOptions)
97                 {
98                         
99                 }
100
101                 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
102                         : base (null, state, cancellationToken, creationOptions)
103                 {
104                         this.function = function;
105                         this.state = state;
106                 }
107
108                 internal Task (Func<object, TResult> function,
109                                object state,
110                                CancellationToken cancellationToken,
111                                TaskCreationOptions creationOptions,
112                                Task parent)
113                 : base (null, state, cancellationToken, creationOptions, parent)
114                 {
115                         this.function = function;
116                         this.state = state;
117                 }
118                 
119                 internal override void InnerInvoke ()
120                 {
121                         if (function != null)
122                                 value = function (state);
123                         
124                         function = null;
125                         state = null;
126                 }
127                 
128                 public Task ContinueWith (Action<Task<TResult>> continuationAction)
129                 {
130                         return ContinueWith (continuationAction, TaskContinuationOptions.None);
131                 }
132                 
133                 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
134                 {
135                         return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
136                 }
137                 
138                 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
139                 {
140                         return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
141                 }
142                 
143                 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
144                 {
145                         return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
146                 }
147                 
148                 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
149                                           TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
150                 {
151                         if (continuationAction == null)
152                                 throw new ArgumentNullException ("continuationFunction");
153                         if (scheduler == null)
154                                 throw new ArgumentNullException ("scheduler");
155
156                         Task t = new Task ((o) => continuationAction ((Task<TResult>)o),
157                                            this,
158                                            cancellationToken,
159                                            GetCreationOptions (continuationOptions),
160                                            this);
161                         ContinueWithCore (t, continuationOptions, scheduler);
162                         
163                         return t;
164                 }
165                 
166                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
167                 {
168                         return ContinueWith<TNewResult> (continuationFunction, TaskContinuationOptions.None);
169                 }
170                 
171                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
172                 {
173                         return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
174                 }
175                 
176                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
177                 {
178                         return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
179                 }
180                 
181                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
182                 {
183                         return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
184                 }
185                 
186                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
187                                                                   CancellationToken cancellationToken,
188                                                                   TaskContinuationOptions continuationOptions,
189                                                                   TaskScheduler scheduler)
190                 {
191                         Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
192                                                                    this,
193                                                                    cancellationToken,
194                                                                    GetCreationOptions (continuationOptions),
195                                                                    this);
196                         ContinueWithCore (t, continuationOptions, scheduler);
197                         
198                         return t;
199                 }
200         }
201 }
202 #endif