Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting task if it fails

Tags:

I'm trying to restart one of multiple tasks if it fails. Im using .ContinueWith(t => { HandleException(t); }, TaskContinuationOptions.OnlyOnFaulted); where the HandleException(t) method should find the task in the array of excisting tasks and create a new one in its place. Only the task passed to my HandleException(t) method is different from original task so I'm not able to find it in my excisting tasks. Also the excisting task is still running at the time the exception is handled.

Example:

using System;
using System.Threading.Tasks;
using System.Threading;

static Task[] tasks;
static void Main(string[] args)
{
    tasks = new Task[2];
    for (int i = 0; i < tasks.Count(); i++)
    {
        tasks[i] = Task.Run(() => { Thread.Sleep(1000); throw new Exception("BOOM"); })
             .ContinueWith(t => { HandleException(t); }
             , TaskContinuationOptions.OnlyOnFaulted);

        Console.WriteLine(String.Format("Task {0} started", tasks[i].Id));
    }

    Console.ReadLine();
}

static void HandleException(Task task)
{
    Console.WriteLine(String.Format("Task {0} stopped", task.Id));

    // Check task status
    for (int i = 0; i < tasks.Count(); i++)
    {
        Console.WriteLine(String.Format("Task {0} status = {1}", i, tasks[i].Status));
    }
    // fFind and restart the task here
    if (tasks.Contains(task))
    {
        int index = Array.IndexOf(tasks, task);
        tasks[index] = Task.Run(() => { Thread.Sleep(1000); throw new Exception("BOOM"); })
         .ContinueWith(t => { HandleException(t); }
         , TaskContinuationOptions.OnlyOnFaulted);

        Console.WriteLine(String.Format("Task {0} started", tasks[index].Id));
    }
}

My application results in:

Task 3 started
Task 6 started
Task 5 stopped
Task 0 status = Running
Task 2 stopped
Task 1 status = Running
Task 0 status = Running
Task 1 status = Running

Since the task passed to the HandleException(t) is different from the original task, it is not found in the tasks[] and thus not restarted. Also looking at the states of the tasks[] it is not yet stopped. How can I properly restart my task?

like image 969
MrEighteen Avatar asked May 18 '17 09:05

MrEighteen


People also ask

Can we restart Task Scheduler service?

Search task scheduler in Windows search. Right click on Task Scheduler and click New Folder and give a name to the folder like Service reset and click OK. Making a folder makes your tasks separate, so you can manage easily. Now right click on new created folder and select Create Task and provide task name.

Can runs scheduled task manually but not automatically?

Most common reason for such problems is permissions: scheduled tasks does NOT always run with your user credentials. If you want scheduled task to run as you you will have to set it up as you or alternative user.


1 Answers

That's because you store continuation task in your array, not original task (that is - you store result of ContinueWith). To fix, store original task instead:

var task = Task.Run(() => { Thread.Sleep(1000); throw new Exception("BOOM");});
task.ContinueWith(t => { HandleException(t); }
         , TaskContinuationOptions.OnlyOnFaulted);
tasks[i] = task;
like image 133
Evk Avatar answered Sep 24 '22 10:09

Evk