17 using System.Threading;
19 namespace erminas.SmartAPI.Utils
21 public static class Wait
25 const int DEFAULT_PERIOD_IN_MS = 100;
26 DefaultRetryPeriod =
new TimeSpan(0, 0, 0, 0, DEFAULT_PERIOD_IN_MS);
29 public static TimeSpan DefaultRetryPeriod {
get;
set; }
31 public static void For(Func<bool> pred, TimeSpan wait)
33 For(pred, wait, DefaultRetryPeriod);
36 public static void For(Func<bool> pred, TimeSpan wait, TimeSpan retry)
40 throw new ArgumentException(
"Retry period must not be greater than wait");
42 var tt =
new TimeOutTracker(wait);
44 var lastTry = DateTime.Now;
45 while (!(isSuccess = pred()) && !tt.HasTimedOut)
47 TimeSpan timeSpan = retry - (DateTime.Now - lastTry);
48 if (timeSpan.TotalMilliseconds > 0)
50 Thread.Sleep(timeSpan);
52 lastTry = DateTime.Now;
56 throw new TimeoutException(
string.Format(
"timed out after waiting for {0}", wait));