| | 1 | | namespace Songhay; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Members for <see cref="Task"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class TaskUtility |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Delays with a <see cref="Timer"/> task for the specified <see cref="TimeSpan"/>. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="timeSpan">The specified <see cref="TimeSpan"/>.</param> |
| | 12 | | /// <param name="actionAfterDelay">The continuation action.</param> |
| | 13 | | public static Task Delay(TimeSpan timeSpan, Action<Task>? actionAfterDelay) => |
| 1 | 14 | | Delay(timeSpan, actionAfterDelay, null); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Delays with a <see cref="Timer"/> task for the specified <see cref="TimeSpan"/>. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="timeSpan">The specified <see cref="TimeSpan"/>.</param> |
| | 20 | | /// <param name="actionAfterDelay">The continuation action.</param> |
| | 21 | | /// <param name="schedulerAfterDelay">The work-queue scheduler.</param> |
| | 22 | | public static Task Delay(TimeSpan timeSpan, Action<Task>? actionAfterDelay, |
| | 23 | | TaskScheduler? schedulerAfterDelay) |
| 1 | 24 | | { |
| 1 | 25 | | var task = Delay(timeSpan); |
| | 26 | |
|
| 1 | 27 | | if (actionAfterDelay != null && schedulerAfterDelay != null) |
| 0 | 28 | | { |
| 0 | 29 | | task.ContinueWith(actionAfterDelay, schedulerAfterDelay); |
| 0 | 30 | | } |
| 1 | 31 | | else if (actionAfterDelay != null) |
| 1 | 32 | | { |
| 1 | 33 | | task.ContinueWith(actionAfterDelay); |
| 1 | 34 | | } |
| | 35 | |
|
| 1 | 36 | | return task; |
| 1 | 37 | | } |
| | 38 | |
|
| | 39 | | static Task Delay(TimeSpan timeSpan) |
| 1 | 40 | | { |
| 1 | 41 | | var tcs = new TaskCompletionSource<bool>(); |
| 1 | 42 | | new Timer(_ => tcs.SetResult(true)).Change(timeSpan, new TimeSpan(-1)); |
| 1 | 43 | | return tcs.Task; |
| 1 | 44 | | } |
| | 45 | | } |