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); }
private void AddToAttributeCache <T>(string key, DoFunc <T> function) { if (!AttributeCache.ContainsKey(key)) { AttributeCache.Add(key, function.Invoke()); } }
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); }
public static T TryFuncIgnoreException <T>(DoFunc <T> func) { try { return(func.Invoke()); } catch { return(default(T)); } }
/// <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)); }
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)); }
private T GetFromAttributeCache <T>(string key, DoFunc <T> function) { AddToAttributeCache(key, function); return((T)AttributeCache[key]); }
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); }
/// <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)); }
public void PlantingBomb(float time, DoFunc e) { DoTime = time; DoFunction += e; }