I created a method which handles a few checks before starting a Task passed as a parameter.
My issue is that the task created there do not behave as expected and are quickly considered as RanToCompletion despite code is still running.
Here is an example:
public Task Main(CancellationToken localToken)
{
try
{
AddToTasker(new Task(async () => await ExtractAllOffer(localToken), localToken), TaskTypes.Extractor);
//this allows to extract only the task for the given task type through the list created in AddToTasker, actual code is not necessary the returned array is correct
Task.WaitAll(GetTasksArray(new TaskTypes[]{TaskTypes.Extractor}), localToken);
IsRunning = false;
}
}
public void AddToTasker(Task task, TaskTypes taskstype)
{
/*
* Whatever code to perform few check before starting the task
* among which referencing the task within a list which holds also the taskstype
*/
task.Start();
}
async private Task ExtractAllOffer(CancellationToken localToken)
{
// *** Do very long Stuff ***
}
The ExtractAllOffer method is a loop, with a few moment where i await
external code completion. At the first await
the Task.WaiAll
terminates and it goes to IsRunning = false
I checked this thread but it does not seem to be the same issue as I correctly use an async task and not an async void.
Also, the code use to run properly before I shifted the Task execution within the AddToTasker method. Before I used to do like this AddToTasker(Task.Run(() => ExtractAllOffer(localToken), localToken), TaskTypes.Extractor);
but I realized I needed to perform checks before starting the Task and needed to factor the checks within the AddToTasker method (I have many calls to this method).
I kind of understand that there is a flaw in how I declare or start my Task but can't figure what.
Help greatly appreciated