Пример #1
0
        public static void Do(string name, DoFunc d)
        {
            var t = Begin(name);

            d();
            t.End();
        }
Пример #2
0
        /// <summary>
        /// Tries the specified action until the result of the action is not equal to <c>default{T}</c>
        /// or the time out is reached.
        /// </summary>
        /// <typeparam name="T">The result type of the action</typeparam>
        /// <param name="func">The action.</param>
        /// <returns>The result of the action of <c>default{T}</c> when time out occured.</returns>
        public T Try <T>(DoFunc <T> func)
        {
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }

            var defaultT     = default(T);
            var timeoutTimer = GetTimer();

            do
            {
                LastException = null;

                try
                {
                    var result = func.Invoke();
                    if (!result.Equals(defaultT))
                    {
                        return(result);
                    }
                }
                catch (Exception e)
                {
                    LastException = e;
                }

                Sleep(SleepTime);
            } while (!timeoutTimer.Elapsed);

            HandleTimeOut();

            return(defaultT);
        }
Пример #3
0
 private void AddToAttributeCache <T>(string key, DoFunc <T> function)
 {
     if (!AttributeCache.ContainsKey(key))
     {
         AttributeCache.Add(key, function.Invoke());
     }
 }
Пример #4
0
        protected void WaitUntil(DoFunc<bool> waitWhile, BuildTimeOutExceptionMessage exceptionMessage)
        {
            if (_waitForCompleteTimer == null) throw new WatiNException("_waitForCompleteTimer not initialized");

            var timeOut = new TryFuncUntilTimeOut(_waitForCompleteTimer) {ExceptionMessage = exceptionMessage};
            timeOut.Try(waitWhile);
        }
Пример #5
0
 public static T TryFuncIgnoreException <T>(DoFunc <T> func)
 {
     try
     {
         return(func.Invoke());
     }
     catch
     {
         return(default(T));
     }
 }
Пример #6
0
        /// <summary>
        /// Waits until the method returns true or false.
        /// </summary>
        /// <param name="func">The function to evaluate.</param>
        /// <param name="exceptionMessage">A function to build an exception message.</param>
        /// <returns>The last function result.</returns>
        /// <exception cref="TimeoutException">Thrown if a timeout occurs.</exception>
        protected bool WaitUntilNotNull(DoFunc <bool?> func, BuildTimeOutExceptionMessage exceptionMessage)
        {
            var result = false;

            WaitUntil(() =>
            {
                var currentResult = func();
                if (currentResult.HasValue)
                {
                    result = currentResult.Value;
                    return(true);
                }
                return(false);
            }, exceptionMessage);
            return(result);
        }
        /// <summary>
        /// Tries the specified action until the result of the action is not equal to <c>default{T}</c>
        /// or the time out is reached.
        /// </summary>
        /// <typeparam name="T">The result type of the action</typeparam>
        /// <param name="func">The action.</param>
        /// <returns>The result of the action of <c>default{T}</c> when time out occured.</returns>
        public T Try <T>(DoFunc <T> func)
        {
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }

            var timeoutTimer = GetTimer();

            var currentSleepTime = TimeSpan.FromMilliseconds(1);

            do
            {
                LastException = null;

                try
                {
                    var result = func.Invoke();
                    if (!Equals(result, default(T)))
                    {
                        return(result);
                    }
                }
                catch (Exception e)
                {
                    LastException = e;
                }

                Sleep(currentSleepTime);

                currentSleepTime += currentSleepTime;
                if (currentSleepTime > SleepTime)
                {
                    currentSleepTime = SleepTime;
                }
            } while (!timeoutTimer.Elapsed);

            HandleTimeOut();

            return(default(T));
        }
Пример #8
0
        public static T TryFuncFailOver <T>(DoFunc <T> func, int numberOfRetries, int sleepTime)
        {
            Exception LastException;

            do
            {
                try
                {
                    var result = func.Invoke();
                    return(result);
                }
                catch (Exception e)
                {
                    LastException = e;
                }

                numberOfRetries -= 1;
                Thread.Sleep(sleepTime);
            } while (numberOfRetries != 0);

            throw LastException;
        }
Пример #9
0
        public static T Try <T>(int timeout, DoFunc <T> func)
        {
            var tryFunc = new TryFuncUntilTimeOut(timeout);

            return(tryFunc.Try(func));
        }
Пример #10
0
 private T GetFromAttributeCache <T>(string key, DoFunc <T> function)
 {
     AddToAttributeCache(key, function);
     return((T)AttributeCache[key]);
 }
Пример #11
0
 private static T GetWithFailOver <T>(DoFunc <T> func)
 {
     return(UtilityClass.GetWithFailOver(func));
 }
Пример #12
0
        protected virtual void WaitUntil(DoFunc<bool> waitWhile, BuildTimeOutExceptionMessage exceptionMessage)
        {
            if (Timer == null)
                throw new WatiNException("_waitForCompleteTimer not initialized");

            var timeOut = new TryFuncUntilTimeOut(Timer) {ExceptionMessage = exceptionMessage};
            timeOut.Try(waitWhile);
        }
Пример #13
0
 /// <summary>
 /// Waits until the method returns true or false.
 /// </summary>
 /// <param name="func">The function to evaluate.</param>
 /// <param name="exceptionMessage">A function to build an exception message.</param>
 /// <returns>The last function result.</returns>
 /// <exception cref="TimeoutException">Thrown if a timeout occurs.</exception>
 protected bool WaitUntilNotNull(DoFunc<bool?> func, BuildTimeOutExceptionMessage exceptionMessage)
 {
     var result = false;
     WaitUntil(() =>
         {
             var currentResult = func();
             if (currentResult.HasValue)
             {
                 result = currentResult.Value;
                 return true;
             }
             return false;
         }, exceptionMessage);
     return result;
 }
Пример #14
0
 public static T GetWithFailOver <T>(DoFunc <T> func)
 {
     return(TryFuncFailOver(func, 5, 50));
 }
Пример #15
0
 public void PlantingBomb(float time, DoFunc e)
 {
     DoTime      = time;
     DoFunction += e;
 }