Applitools.Utils.CommonUtils.Retry C# (CSharp) Method

Retry() public static method

Runs the input action upon each expiry of interval until action returns true or timeout expires.
public static Retry ( TimeSpan timeout, TimeSpan interval, Func action ) : bool
timeout TimeSpan
interval TimeSpan
action Func
return bool
        public static bool Retry(TimeSpan timeout, TimeSpan interval, Func<bool> action)
        {
            ArgumentGuard.NotNull(action, nameof(action));

            Stopwatch sw = Stopwatch.StartNew();
            while (true)
            {
                try
                {
                    if (action())
                    {
                        return true;
                    }
                }
                catch
                {
                }

                if (sw.Elapsed > timeout)
                {
                    return false;
                }

                Thread.Sleep(interval);
            }
        }