using System; using System.Diagnostics; using System.Threading; namespace Umbraco.Core { public static class DelegateExtensions { public static Attempt RetryUntilSuccessOrTimeout(this Func> task, TimeSpan timeout, TimeSpan pause) { if (pause.TotalMilliseconds < 0) { throw new ArgumentException("pause must be >= 0 milliseconds"); } var stopwatch = Stopwatch.StartNew(); do { var result = task(); if (result) { return result; } Thread.Sleep((int)pause.TotalMilliseconds); } while (stopwatch.Elapsed < timeout); return Attempt.Fail(); } public static Attempt RetryUntilSuccessOrMaxAttempts(this Func> task, int totalAttempts, TimeSpan pause) { if (pause.TotalMilliseconds < 0) { throw new ArgumentException("pause must be >= 0 milliseconds"); } int attempts = 0; do { attempts++; var result = task(attempts); if (result) { return result; } Thread.Sleep((int)pause.TotalMilliseconds); } while (attempts < totalAttempts); return Attempt.Fail(); } } }