Skip to content

Generic methods for retrying functions and actions in .NET

License

Notifications You must be signed in to change notification settings

maxinfet/Mulligan

Repository files navigation

Build Status

Introduction

Mulligan is a library that defines generic retry logic. This code is inspired by the retry code in FlaUI, TestStack.White and some feed back from a co-worker.

Why would you need this?

The reason for using this is if your code is interacting with an entity that is timing based. For example when working with UI automation the UI will update at different speeds and you would want to be able to retry finding a control.

Usage

This will retry the action until it passes or 1 second passes. Then it will return a RetryResult that contains information about each retry that was attempted.

Action action = () => //Do something
RetryResult retryResult = Retry.While(action, TimeSpan.FromSeconds(1));

int retryCount = retryResult.Count;
bool isCompletedSuccessfully = retryResult.IsCompletedSuccessfully;

This will retry the function until the predicate evaluates false. The predicate should return true if you want to retry your function. The Retry.While will then return a RetryResult that contains the result and information about each retry it attempted.

Func<T> function = () => //Do something
Predicate<T> shouldRetry = resultOfFunction => //use the result of the function to evaluate if you should retry
RetryResult<T> retryResult = Retry.While(shouldRetry, function, TimeSpan.FromSeconds(1));

int retryCount = retryResult.Count;
bool isCompletedSuccessfully = retryResult.IsCompletedSuccessfully;
T result = retryResult.Result.Value;

About

Generic methods for retrying functions and actions in .NET

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages