How to implement retry logic with Task Parallel Li

2020-02-12 02:20发布

问题:

This question already has answers here:
Closed 7 years ago.

Possible Duplicate:
Retry a task multiple times based on user input in case of an exception in task

I'm looking for a way to implement retry logic in TPL. I would like to have a generic function/class that will be able to return a Task which will execute a given action and in case of an exception will retry the task, up to the given retry count. I tried playing with ContinueWith and have the callback create a new task in case of an exception, but it seems that it will only work for fixed amount of retries. Any suggestions?

    private static void Main()
    {
        Task<int> taskWithRetry = CreateTaskWithRetry(DoSometing, 10);
        taskWithRetry.Start();
        // ...

    }

    private static int DoSometing()
    {
        throw new NotImplementedException();
    }

    private static Task<T> CreateTaskWithRetry<T>(Func<T> action, int retryCount)
    {

    }

回答1:

Any reason to do anything special to do with the TPL? Why not just make a wrapper for Func<T> itself?

public static Func<T> Retry(Func<T> original, int retryCount)
{
    return () =>
    {
        while (true)
        {
            try
            {
                return original();
            }
            catch (Exception e)
            {
                if (retryCount == 0)
                {
                    throw;
                }
                // TODO: Logging
                retryCount--;
            }
        }
    };
}

Note that you may want to add a ShouldRetry(Exception) method to allow certain exceptions (e.g. cancellation) abort without the retry.



回答2:

private static Task<T> CreateTaskWithRetry<T>(Func<T> action, int retryCount)
{
    Func<T> retryAction = () =>
    {
        int attemptNumber = 0;
        do
        {
            try
            {
                attemptNumber++;
                return action();
            }
            catch (Exception exception) // use your the exception that you need
            {
                // log error if needed
                if (attemptNumber == retryCount)
                    throw;
            }
        }
        while (attemptNumber < retryCount);

        return default(T);
    };

    return new Task<T>(retryAction);
}