4 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
28 namespace System
.Threading
.Tasks
31 public class Task
<TResult
>: Task
34 static TaskFactory
<TResult
> factory
= new TaskFactory
<TResult
> ();
36 Func
<object, TResult
> function
;
39 public TResult Result
{
50 public static new TaskFactory
<TResult
> Factory
{
56 public Task (Func
<TResult
> function
) : this (function
, TaskCreationOptions
.None
)
61 public Task (Func
<TResult
> function
, CancellationToken token
)
62 : this ((o
) => function (), null, token
, TaskCreationOptions
.None
)
67 public Task (Func
<TResult
> function
, TaskCreationOptions options
)
68 : this ((o
) => function (), null, CancellationToken
.None
, options
)
73 public Task (Func
<TResult
> function
, CancellationToken token
, TaskCreationOptions options
)
74 : this ((o
) => function (), null, token
, options
)
79 public Task (Func
<object, TResult
> function
, object state
) : this (function
, state
, TaskCreationOptions
.None
)
84 public Task (Func
<object, TResult
> function
, object state
, CancellationToken token
)
85 : this (function
, state
, token
, TaskCreationOptions
.None
)
90 public Task (Func
<object, TResult
> function
, object state
, TaskCreationOptions options
)
91 : this (function
, state
, CancellationToken
.None
, options
)
96 public Task (Func
<object, TResult
> function
, object state
, CancellationToken token
, TaskCreationOptions options
)
97 : base (null, state
, token
, options
)
99 this.function
= function
;
103 internal override void InnerInvoke ()
105 if (function
!= null)
106 value = function (state
);
112 public Task
ContinueWith (Action
<Task
<TResult
>> a
)
114 return ContinueWith (a
, TaskContinuationOptions
.None
);
117 public Task
ContinueWith (Action
<Task
<TResult
>> a
, TaskContinuationOptions options
)
119 return ContinueWith (a
, CancellationToken
.None
, options
, TaskScheduler
.Current
);
122 public Task
ContinueWith (Action
<Task
<TResult
>> a
, CancellationToken token
)
124 return ContinueWith (a
, token
, TaskContinuationOptions
.None
, TaskScheduler
.Current
);
127 public Task
ContinueWith (Action
<Task
<TResult
>> a
, TaskScheduler scheduler
)
129 return ContinueWith (a
, CancellationToken
.None
, TaskContinuationOptions
.None
, scheduler
);
132 public Task
ContinueWith (Action
<Task
<TResult
>> a
, CancellationToken token
,
133 TaskContinuationOptions options
, TaskScheduler scheduler
)
135 Task t
= new Task ((o
) => a ((Task
<TResult
>)o
), this, token
, GetCreationOptions (options
));
136 ContinueWithCore (t
, options
, scheduler
);
141 public Task
<TNewResult
> ContinueWith
<TNewResult
> (Func
<Task
<TResult
>, TNewResult
> a
)
143 return ContinueWith
<TNewResult
> (a
, TaskContinuationOptions
.None
);
146 public Task
<TNewResult
> ContinueWith
<TNewResult
> (Func
<Task
<TResult
>, TNewResult
> a
, CancellationToken token
)
148 return ContinueWith
<TNewResult
> (a
, token
, TaskContinuationOptions
.None
, TaskScheduler
.Current
);
151 public Task
<TNewResult
> ContinueWith
<TNewResult
> (Func
<Task
<TResult
>, TNewResult
> a
, TaskContinuationOptions options
)
153 return ContinueWith
<TNewResult
> (a
, CancellationToken
.None
, options
, TaskScheduler
.Current
);
156 public Task
<TNewResult
> ContinueWith
<TNewResult
> (Func
<Task
<TResult
>, TNewResult
> a
, TaskScheduler scheduler
)
158 return ContinueWith
<TNewResult
> (a
, CancellationToken
.None
, TaskContinuationOptions
.None
, scheduler
);
161 public Task
<TNewResult
> ContinueWith
<TNewResult
> (Func
<Task
<TResult
>, TNewResult
> a
, CancellationToken token
,
162 TaskContinuationOptions options
,
163 TaskScheduler scheduler
)
165 Task
<TNewResult
> t
= new Task
<TNewResult
> ((o
) => a ((Task
<TResult
>)o
), this, token
, GetCreationOptions (options
));
166 ContinueWithCore (t
, options
, scheduler
);