Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement retry logic with Task Parallel Library(TPL) [duplicate]

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)
    {

    }
like image 201
Amos Avatar asked May 22 '11 18:05

Amos


1 Answers

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.

like image 159
Jon Skeet Avatar answered Oct 22 '22 15:10

Jon Skeet