@@ -34,15 +34,15 @@ internal static Task CreateContinuationTask(Task task, Action onSuccess, Action<
3434 TaskCompletionSource < object > completion = new TaskCompletionSource < object > ( ) ;
3535 ContinueTaskWithState ( task , completion ,
3636 state : Tuple . Create ( onSuccess , onFailure , completion ) ,
37- onSuccess : ( state ) =>
37+ onSuccess : static ( object state ) =>
3838 {
3939 var parameters = ( Tuple < Action , Action < Exception > , TaskCompletionSource < object > > ) state ;
4040 Action success = parameters . Item1 ;
4141 TaskCompletionSource < object > taskCompletionSource = parameters . Item3 ;
4242 success ( ) ;
4343 taskCompletionSource . SetResult ( null ) ;
4444 } ,
45- onFailure : ( exception , state ) =>
45+ onFailure : static ( Exception exception , object state ) =>
4646 {
4747 var parameters = ( Tuple < Action , Action < Exception > , TaskCompletionSource < object > > ) state ;
4848 Action < Exception > failure = parameters . Item2 ;
@@ -64,7 +64,7 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
6464 {
6565 var completion = new TaskCompletionSource < object > ( ) ;
6666 ContinueTaskWithState ( task , completion , state ,
67- onSuccess : ( continueState ) =>
67+ onSuccess : ( object continueState ) =>
6868 {
6969 onSuccess ( continueState ) ;
7070 completion . SetResult ( null ) ;
@@ -81,12 +81,12 @@ internal static Task CreateContinuationTask<T1, T2>(Task task, Action<T1, T2> on
8181 }
8282
8383 internal static void ContinueTask ( Task task ,
84- TaskCompletionSource < object > completion ,
85- Action onSuccess ,
86- Action < Exception > onFailure = null ,
87- Action onCancellation = null ,
88- Func < Exception , Exception > exceptionConverter = null
89- )
84+ TaskCompletionSource < object > completion ,
85+ Action onSuccess ,
86+ Action < Exception > onFailure = null ,
87+ Action onCancellation = null ,
88+ Func < Exception , Exception > exceptionConverter = null
89+ )
9090 {
9191 task . ContinueWith (
9292 tsk =>
@@ -145,7 +145,7 @@ internal static void ContinueTaskWithState(Task task,
145145 )
146146 {
147147 task . ContinueWith (
148- tsk =>
148+ ( Task tsk , object state2 ) =>
149149 {
150150 if ( tsk . Exception != null )
151151 {
@@ -156,7 +156,7 @@ internal static void ContinueTaskWithState(Task task,
156156 }
157157 try
158158 {
159- onFailure ? . Invoke ( exc , state ) ;
159+ onFailure ? . Invoke ( exc , state2 ) ;
160160 }
161161 finally
162162 {
@@ -167,7 +167,7 @@ internal static void ContinueTaskWithState(Task task,
167167 {
168168 try
169169 {
170- onCancellation ? . Invoke ( state ) ;
170+ onCancellation ? . Invoke ( state2 ) ;
171171 }
172172 finally
173173 {
@@ -178,14 +178,16 @@ internal static void ContinueTaskWithState(Task task,
178178 {
179179 try
180180 {
181- onSuccess ( state ) ;
181+ onSuccess ( state2 ) ;
182182 }
183183 catch ( Exception e )
184184 {
185185 completion . SetException ( e ) ;
186186 }
187187 }
188- } , TaskScheduler . Default
188+ } ,
189+ state : state ,
190+ scheduler : TaskScheduler . Default
189191 ) ;
190192 }
191193
@@ -205,25 +207,42 @@ internal static void WaitForCompletion(Task task, int timeout, Action onTimeout
205207 }
206208 if ( ! task . IsCompleted )
207209 {
208- task . ContinueWith ( t => { var ignored = t . Exception ; } ) ; //Ensure the task does not leave an unobserved exception
209- if ( onTimeout != null )
210- {
211- onTimeout ( ) ;
212- }
210+ task . ContinueWith ( static t => { var ignored = t . Exception ; } ) ; //Ensure the task does not leave an unobserved exception
211+ onTimeout ? . Invoke ( ) ;
213212 }
214213 }
215214
216- internal static void SetTimeoutException ( TaskCompletionSource < object > completion , int timeout , Func < Exception > exc , CancellationToken ctoken )
215+ internal static void SetTimeoutException ( TaskCompletionSource < object > completion , int timeout , Func < Exception > onFailure , CancellationToken ctoken )
217216 {
218217 if ( timeout > 0 )
219218 {
220- Task . Delay ( timeout * 1000 , ctoken ) . ContinueWith ( ( tsk ) =>
221- {
222- if ( ! tsk . IsCanceled && ! completion . Task . IsCompleted )
219+ Task . Delay ( timeout * 1000 , ctoken ) . ContinueWith (
220+ ( Task task ) =>
223221 {
224- completion . TrySetException ( exc ( ) ) ;
222+ if ( ! task . IsCanceled && ! completion . Task . IsCompleted )
223+ {
224+ completion . TrySetException ( onFailure ( ) ) ;
225+ }
225226 }
226- } ) ;
227+ ) ;
228+ }
229+ }
230+
231+ internal static void SetTimeoutExceptionWithState ( TaskCompletionSource < object > completion , int timeout , object state , Func < object , Exception > onFailure , CancellationToken cancellationToken )
232+ {
233+ if ( timeout > 0 )
234+ {
235+ Task . Delay ( timeout * 1000 , cancellationToken ) . ContinueWith (
236+ ( Task task , object state ) =>
237+ {
238+ if ( ! task . IsCanceled && ! completion . Task . IsCompleted )
239+ {
240+ completion . TrySetException ( onFailure ( state ) ) ;
241+ }
242+ } ,
243+ state : state ,
244+ cancellationToken : CancellationToken . None
245+ ) ;
227246 }
228247 }
229248 }
0 commit comments