Retry method execution on failure? I am working on an application that is required to be constantly running, and as a requirement should make multiple attempts for particular processes such as web API/database calls, just in case network connectivity goes down for a moment, or something similar.
In this case, I'm constantly having to write a basic algorithm:
for (int attempt = 0; i &lt; Config.AttemptCount; i++) {
    try {
        doSomething...;
        return true;
    } catch (Exception e) {
        Log(e);
    } finally {
        if (Config.WaitTimeForRetry &gt; 0)
            Thread.Sleep(Config.WaitTimeForRetry);
    }
}

return false;

This of course is the most basic example, but there are others where I'm returning actual objects, performing heavy operations, etc. I recently got the idea to make a single method that could perform this algorithm for me so I can focus on the overall functionality with each implementation instead. With that said, I present to you, a method I need a better name for, and probably needs a much better implementation, or should be replaced by something pre-existing if such a thing exists because I re-invent the wheel a lot, but none-the-less, it's here .
public static TOut RunProcessWithMultipleAttempts&lt;TIn, TOut&gt;(int numberOfAttempts, int waitTimeInMilliseconds, TIn input, Func&lt;TIn, TOut&gt; process) {
    TOut result = default;
    while (numberOfAttempts-- &gt; 0) {
        try {
            result = process.Invoke(input);
            break;
        } catch (Exception e) {
            Log(e);
        } finally {
            if (waitTimeInMilliseconds &gt; 0)
                Thread.Sleep(waitTimeInMilliseconds);
        }
    }

    return result;
}

To use this, I would write a method that does the work I wish to be performed:
static int attempts = 0;
private static string TrySomething(int i) {
    for (int index = 0; index &lt; i; index++)
        if (attempts++ &lt; 2)
            throw new Exception(&quot;Your request was denied.&quot;);

    return &quot;Your request was successful.&quot;;
}

I would then utilize the RunProcessWithMultipleAttempts method to execute it:
static void Main(string[] args) {
    Console.WriteLine(RunProcessWithMultipleAttempts(3, 1000, 10, TrySomething));

    // Prevent the console from closing.
    Console.ReadKey();
}

In this example, replace the Log(e) call with Console.WriteLine(e) to allow it to work on your local machine.

A few questions in my mind:

How hard is this to scale?
What are the downsides to this method?
What alternatives are available?
Is this code easy to understand?
Not only in final usage, but initial implementation?

Oh, and perhaps my biggest question if I were to keep this implementation around:

What would a more concise and clear name be for this method?
How hard is this to scale?
Sorry but I don't understand what do you mean here. How do you want to scale a method?
What are the downsides to this method?

It does not work with Action

It does not work with Func&lt;T1, T2, TResult&gt;

It does not work with Func&lt;Task&gt;

It does not work with Func&lt;Task&lt;T&gt;&gt;

It blocks the calling thread unnecessary (Thread.Sleep)

You must throw exception to enforce retry (can't use any other condition, like Result != true)

It swallows the exception

in the worst case if all attempts fail then it will return a default instead of the exception


It does not allow to use exponential back-off algorithm

It does not allow infinite retry (performing yet another attempt until it succeeds)

It does not utilize Cancellation concept

It does not provide debug information (like how many times the retry has been performed)


What alternatives are available?

Polly's Retry &lt;&lt; The suggested
EnterpriseLibrary.TransientFaultHandling.Core

Is this code easy to understand?
For me, yes.
Not only in final usage, but initial implementation?
Is this a question?
What would a more concise and clear name be for this method?

ExecuteWithRetry
ExecuteAtMostNTimes
RetryFunction
PerformCallWithRetryPolicy
etc.