Daniel Park Daniel Park - 1 year ago 95
C# Question

Restarting a task in the background if certain errors occur

I am using some REST requests using Mono.Mac (3.2.3) to communicate with a server, and as a retry mechanism I am quietly attempting to give the HTTP actions multiple tries if they fail, or time out.

I have the following;

var tries = 0;
while (tries <= ALLOWED_TRIES)
if (!postTask.Wait(Timeout))
throw new TimeoutException("Operation timed out");
} catch (Exception e) {
if (tries > ALLOWED_TRIES)
throw new Exception("Failed to access Resource.", e);

Where the task uses parameters of the parent method like so;

var postTask = new Task<HttpWebResponse>(() => {return someStuff(foo, bar);},

The problem seems to be that the task does not want to be run again with
after it's first completion (and subsequent failure). Is there a simple way of doing this, or am I misusing tasks in this way? Is there some sort of method that resets the task to its initial state, or am I better off using a factory of some sort?

Answer Source

You're indeed misusing the Task here, for a few reasons:

  • You cannot run the same task more than once. When it's done, it's done.

  • It is not recommended to construct a Task object manually, there's Task.Run and Task.Factory.Start for that.

  • You should not use Task.Run/Task.Factory.Start for a task which does IO-bound work. They are intended for CPU-bound work, as they "borrow" a thread from ThreadPool to execute the task action. Instead, use pure async Task-based APIs for this, which do not need a dedicate thread to complete.

For example, below you can call GetResponseWithRetryAsync from the UI thread and still keep the UI responsive:

async Task<HttpWebResponse> GetResponseWithRetryAsync(string url, int retries)
    if (retries < 0)
        throw new ArgumentOutOfRangeException();

    var request = WebRequest.Create(url);
    while (true)
            var result = await request.GetResponseAsync();
            return (HttpWebResponse)result;
        catch (Exception ex)
            if (--retries == 0)
                throw; // rethrow last error
            // otherwise, log the error and retry
            Debug.Print("Retrying after error: " + ex.Message);

More reading:

"Task.Factory.StartNew" vs "new Task(...).Start".

Task.Run vs Task.Factory.StartNew.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download