TaskExtensions.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Threading.Tasks
  5. {
  6. internal static class TaskExtensions
  7. {
  8. public static Task ContinueWithState<TState>(this Task task, Action<Task, TState> continuationAction, TState state, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions)
  9. {
  10. return task.ContinueWith(
  11. (t, tupleObject) =>
  12. {
  13. var (closureAction, closureState) = ((Action<Task, TState>, TState))tupleObject!;
  14. closureAction(t, closureState);
  15. },
  16. (continuationAction, state),
  17. cancellationToken,
  18. continuationOptions,
  19. TaskScheduler.Default);
  20. }
  21. public static Task ContinueWithState<TResult, TState>(this Task<TResult> task, Action<Task<TResult>, TState> continuationAction, TState state, CancellationToken cancellationToken)
  22. {
  23. return task.ContinueWith(
  24. (t, tupleObject) =>
  25. {
  26. var (closureAction, closureState) = ((Action<Task<TResult>, TState>, TState))tupleObject!;
  27. closureAction(t, closureState);
  28. },
  29. (continuationAction, state),
  30. cancellationToken);
  31. }
  32. public static Task ContinueWithState<TResult, TState>(this Task<TResult> task, Action<Task<TResult>, TState> continuationAction, TState state, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions)
  33. {
  34. return task.ContinueWith(
  35. (t, tupleObject) =>
  36. {
  37. var (closureAction, closureState) = ((Action<Task<TResult>, TState>, TState))tupleObject!;
  38. closureAction(t, closureState);
  39. },
  40. (continuationAction, state),
  41. cancellationToken,
  42. continuationOptions,
  43. TaskScheduler.Default);
  44. }
  45. }
  46. }