Exemple #1
0
        public static void Do(string name, DoFunc d)
        {
            var t = Begin(name);

            d();
            t.End();
        }
        /// <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);
        }
Exemple #3
0
 private void AddToAttributeCache <T>(string key, DoFunc <T> function)
 {
     if (!AttributeCache.ContainsKey(key))
     {
         AttributeCache.Add(key, function.Invoke());
     }
 }
Exemple #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);
        }
Exemple #5
0
 public static T TryFuncIgnoreException <T>(DoFunc <T> func)
 {
     try
     {
         return(func.Invoke());
     }
     catch
     {
         return(default(T));
     }
 }
Exemple #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));
        }
Exemple #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;
        }
        public static T Try <T>(int timeout, DoFunc <T> func)
        {
            var tryFunc = new TryFuncUntilTimeOut(timeout);

            return(tryFunc.Try(func));
        }
Exemple #10
0
 private T GetFromAttributeCache <T>(string key, DoFunc <T> function)
 {
     AddToAttributeCache(key, function);
     return((T)AttributeCache[key]);
 }
Exemple #11
0
 private static T GetWithFailOver <T>(DoFunc <T> func)
 {
     return(UtilityClass.GetWithFailOver(func));
 }
        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);
        }
Exemple #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;
 }
 public static T GetWithFailOver <T>(DoFunc <T> func)
 {
     return(TryFuncFailOver(func, 5, 50));
 }
Exemple #15
0
 public void PlantingBomb(float time, DoFunc e)
 {
     DoTime      = time;
     DoFunction += e;
 }