| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Collections; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace GDX.Threading |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Some useful wait for methods to control program flow. |
| | 13 | | /// </summary> |
| | 14 | | public static class WaitFor |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// One second worth of milliseconds. |
| | 18 | | /// </summary> |
| | 19 | | public const int OneSecond = 1000; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Two seconds worth of milliseconds. |
| | 23 | | /// </summary> |
| | 24 | | public const int TwoSeconds = 2000; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Five seconds worth of milliseconds. |
| | 28 | | /// </summary> |
| | 29 | | public const int FiveSeconds = 5000; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Ten seconds worth of milliseconds. |
| | 33 | | /// </summary> |
| | 34 | | public const int TenSeconds = 10000; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Thirty seconds worth of milliseconds. |
| | 38 | | /// </summary> |
| | 39 | | public const int ThirtySeconds = 30000; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// One minute worth of milliseconds. |
| | 43 | | /// </summary> |
| | 44 | | public const int OneMinute = 60000; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Ten minutes worth of milliseconds. |
| | 48 | | /// </summary> |
| | 49 | | public const int TenMinutes = 600000; |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Wait using an <see cref="IEnumerator" />. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="milliseconds">The number of milliseconds to wait for.</param> |
| | 55 | | /// <returns>Yields null values.</returns> |
| | 56 | | public static IEnumerator GetEnumerator(int milliseconds) |
| 9 | 57 | | { |
| 9 | 58 | | Stopwatch stopwatch = new Stopwatch(); |
| 9 | 59 | | stopwatch.Restart(); |
| 4926 | 60 | | while (stopwatch.ElapsedMilliseconds < milliseconds) |
| 4917 | 61 | | { |
| 4917 | 62 | | yield return null; |
| 4917 | 63 | | } |
| | 64 | |
|
| 9 | 65 | | stopwatch.Stop(); |
| 9 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Wait asynchronously. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="milliseconds">The number of milliseconds to wait for.</param> |
| | 72 | | public static async Task GetTask(int milliseconds) |
| 1 | 73 | | { |
| 3 | 74 | | await Task.Run(() => |
| 1 | 75 | | { |
| 1 | 76 | | Task.Delay(milliseconds).Wait(); |
| 1 | 77 | | }); |
| 1 | 78 | | } |
| | 79 | | } |
| | 80 | | } |