diff --git a/src/CuteUtils.Tests/Misc/WaitTests.cs b/src/CuteUtils.Tests/Misc/WaitTests.cs index 634a7ee..5bbc4ad 100644 --- a/src/CuteUtils.Tests/Misc/WaitTests.cs +++ b/src/CuteUtils.Tests/Misc/WaitTests.cs @@ -36,7 +36,7 @@ public class WaitTests await Task.Delay(200); flag = true; }); - Wait.Until(() => flag, TimeSpan.FromMilliseconds(50), TimeSpan.FromSeconds(2)); + Wait.Until(() => flag, TimeSpan.FromMilliseconds(50)); Assert.IsTrue(flag); } @@ -87,7 +87,7 @@ public class WaitTests handler(42); }); } - int result = await Wait.WaitForEventAsync((Action>)subscribe, TimeSpan.FromSeconds(2)); + int result = await Wait.ForEventAsync((Action>)subscribe, TimeSpan.FromSeconds(2)); Assert.AreEqual(42, result); } @@ -100,7 +100,7 @@ public class WaitTests } _ = await Assert.ThrowsExactlyAsync(async () => { - _ = await Wait.WaitForEventAsync((Action>)subscribe, TimeSpan.FromMilliseconds(200)); + _ = await Wait.ForEventAsync((Action>)subscribe, TimeSpan.FromMilliseconds(200)); }); } @@ -115,7 +115,7 @@ public class WaitTests handler(); }); } - Wait.WaitForEvent(subscribe, TimeSpan.FromSeconds(2)); + Wait.ForEvent(subscribe, TimeSpan.FromSeconds(2)); } [TestMethod] @@ -127,7 +127,7 @@ public class WaitTests } _ = Assert.ThrowsExactly(() => { - Wait.WaitForEvent(subscribe, TimeSpan.FromMilliseconds(200)); + Wait.ForEvent(subscribe, TimeSpan.FromMilliseconds(200)); }); } @@ -142,7 +142,7 @@ public class WaitTests handler("hello"); }); } - string result = Wait.WaitForEvent((Action>)subscribe, TimeSpan.FromSeconds(2)); + string result = Wait.ForEvent((Action>)subscribe, TimeSpan.FromSeconds(2)); Assert.AreEqual("hello", result); } @@ -155,7 +155,7 @@ public class WaitTests } _ = Assert.ThrowsExactly(() => { - _ = Wait.WaitForEvent((Action>)subscribe, TimeSpan.FromMilliseconds(200)); + _ = Wait.ForEvent((Action>)subscribe, TimeSpan.FromMilliseconds(200)); }); } } \ No newline at end of file diff --git a/src/CuteUtils/Misc/Try.cs b/src/CuteUtils/Misc/Try.cs index bd41ed6..5656588 100644 --- a/src/CuteUtils/Misc/Try.cs +++ b/src/CuteUtils/Misc/Try.cs @@ -124,7 +124,7 @@ public static class Try catch (Exception ex) { lastException = ex; - System.Threading.Thread.Sleep(delay.Value); + Thread.Sleep(delay.Value); } } throw new InvalidOperationException($"Failed after {maxRetries} attempts.", lastException); diff --git a/src/CuteUtils/Misc/WaitUntil.cs b/src/CuteUtils/Misc/WaitUntil.cs index 470fc06..4c84876 100644 --- a/src/CuteUtils/Misc/WaitUntil.cs +++ b/src/CuteUtils/Misc/WaitUntil.cs @@ -1,4 +1,6 @@ -namespace CuteUtils.Misc; +using System.Diagnostics; + +namespace CuteUtils.Misc; /// /// Provides utility methods for waiting on conditions or events with optional timeouts and intervals. @@ -16,15 +18,20 @@ public static class Wait { timeout ??= TimeSpan.MaxValue; interval ??= TimeSpan.FromMilliseconds(100); - DateTime endTime = DateTime.UtcNow.Add(timeout.Value); - while (!condition() && DateTime.UtcNow < endTime) + + Stopwatch sw = Stopwatch.StartNew(); + + while (sw.Elapsed < timeout.Value) { + if (condition()) + { + return; + } + await Task.Delay(interval.Value); } - if (!condition()) - { - throw new TimeoutException("The condition was not met within the specified timeout."); - } + + throw new TimeoutException("The condition was not met within the specified timeout."); } /// @@ -38,15 +45,20 @@ public static class Wait { timeout ??= TimeSpan.MaxValue; interval ??= TimeSpan.FromMilliseconds(100); - DateTime endTime = DateTime.UtcNow.Add(timeout.Value); - while (!condition() && DateTime.UtcNow < endTime) + + Stopwatch sw = Stopwatch.StartNew(); + + while (sw.Elapsed < timeout.Value) { - System.Threading.Thread.Sleep(interval.Value); - } - if (!condition()) - { - throw new TimeoutException("The condition was not met within the specified timeout."); + if (condition()) + { + return; + } + + Thread.Sleep(interval.Value); } + + throw new TimeoutException("The condition was not met within the specified timeout."); } /// @@ -88,7 +100,7 @@ public static class Wait /// The type of the event argument. /// A task that completes with the event argument when the event is raised. /// Thrown if the event is not raised within the timeout. - public static async Task WaitForEventAsync(Action> subscribe, TimeSpan? timeout = null) + public static async Task ForEventAsync(Action> subscribe, TimeSpan? timeout = null) { timeout ??= TimeSpan.MaxValue; @@ -121,7 +133,7 @@ public static class Wait /// An action that subscribes a handler to the event. /// The maximum time to wait. Defaults to infinite. /// Thrown if the event is not raised within the timeout. - public static void WaitForEvent(Action subscribe, TimeSpan? timeout = null) + public static void ForEvent(Action subscribe, TimeSpan? timeout = null) { timeout ??= TimeSpan.MaxValue; TaskCompletionSource tcs = new TaskCompletionSource(); @@ -152,7 +164,7 @@ public static class Wait /// The type of the event argument. /// The event argument when the event is raised. /// Thrown if the event is not raised within the timeout. - public static T WaitForEvent(Action> subscribe, TimeSpan? timeout = null) + public static T ForEvent(Action> subscribe, TimeSpan? timeout = null) { timeout ??= TimeSpan.MaxValue; TaskCompletionSource tcs = new TaskCompletionSource();