private void ExecuteAction(TryContext context) { // if action fails an exception is thrown context.Action(context); // so now action must habe been successful context.Succeeded = true; }
/// <summary> /// executes the specified action and returns the corresponsing TryContext /// also see the extensions method TryExtensions.Execute which returns the result of an action rather than a trycontext /// </summary> /// <param name="action"></param> /// <returns></returns> public TryContext Execute(Action <TryContext> action) { var tryContext = new TryContext() { Action = action, FailedCount = 0, Try = this, }; // as long as action should repeat while (RepeatStrategy.ComputeRepeat(tryContext)) { try { ExecuteAction(tryContext); // action was successfully executed return(tryContext); } catch (Exception e) { //increase failedcount tryContext.FailedCount++; tryContext.AddException(e); // try to handle exceptions bool handled = false; foreach (var handler in exceptionHandlers) { handled |= handler.HandleException(tryContext); } // if exception is unhandled stop retrying if (!handled) { if (FailQueitly) { break; } //throw exception directly throw e; } } // do not wait if action should not be repeated anyways if (!RepeatStrategy.ComputeRepeat(tryContext)) { break; } // wait for a strategic amount of time before next retry var wait = WaitingStrategy.ComputeDelay(tryContext); Thread.Sleep(wait); tryContext.WaitedTime += wait; } // throw exception if FailQuietly is false if (!FailQueitly && !tryContext) { throw new AggregateException("failed to execute", tryContext.Exceptions); } return(tryContext); }