/// <summary>
        /// Invokes specified invoker inside a loop with try-catch/everything and retries until reaches timeout or until each of the following is true:
        /// - The invoker does not throw exception.
        /// - If the additional condition delegate is specified, it must return true.
        /// </summary>
        /// <param name="invoker">The method to invoke</param>
        /// <param name="stopCondition">If specified, to break from the loop, it must be true. The condition delegate must not throw.</param>
        /// <param name="timeout">Timeout to stop retries after.</param>
        /// <returns>True on success, false on timeout.</returns>
        private bool InvokeWithRetry(MethodInvoker invoker, BoolMethodInvoker stopCondition, TimeSpan timeout)
        {
            Debug.Assert(invoker != null);

            Stopwatch timer = Stopwatch.StartNew();
            bool hasTimedOut = true;
            try
            {
                do
                {
                    bool isExceptionThrown = false;
                    try
                    {
                        invoker.Invoke();
                    }
                    catch
                    {
                        isExceptionThrown = true;
                    }

                    // If there was no exception, also check for stop condition.
                    // Note: condition.Invoke() must not throw. If it throws, we also throw from here.
                    if (!isExceptionThrown &&
                        (stopCondition == null || stopCondition.Invoke()))
                    {
                        hasTimedOut = false;
                        break;
                    }

                    Thread.Sleep(RegistrySettings.BaseSleepDuration);   // This is to prevent 100% CPU consumption.
                } while (timer.Elapsed < timeout);
            }
            finally
            {
                timer.Stop();
            }

            return !hasTimedOut;
        }
示例#2
0
        /// <summary>
        /// Invokes specified invoker inside a loop with try-catch/everything and retries until reaches timeout or until each of the following is true:
        /// - The invoker does not throw exception.
        /// - If the additional condition delegate is specified, it must return true.
        /// </summary>
        /// <param name="invoker">The method to invoke</param>
        /// <param name="stopCondition">If specified, to break from the loop, it must be true. The condition delegate must not throw.</param>
        /// <param name="timeout">Timeout to stop retries after.</param>
        /// <returns>True on success, false on timeout.</returns>
        private bool InvokeWithRetry(MethodInvoker invoker, BoolMethodInvoker stopCondition, TimeSpan timeout)
        {
            Debug.Assert(invoker != null);

            Stopwatch timer       = Stopwatch.StartNew();
            bool      hasTimedOut = true;

            try
            {
                do
                {
                    bool isExceptionThrown = false;
                    try
                    {
                        invoker.Invoke();
                    }
                    catch
                    {
                        isExceptionThrown = true;
                    }

                    // If there was no exception, also check for stop condition.
                    // Note: condition.Invoke() must not throw. If it throws, we also throw from here.
                    if (!isExceptionThrown &&
                        (stopCondition == null || stopCondition.Invoke()))
                    {
                        hasTimedOut = false;
                        break;
                    }

                    Thread.Sleep(RegistrySettings.BaseSleepDuration);   // This is to prevent 100% CPU consumption.
                } while (timer.Elapsed < timeout);
            }
            finally
            {
                timer.Stop();
            }

            return(!hasTimedOut);
        }