I have some long running code:
Action GetSomeAction()=> () => {
//some code that takes a long time...
};
I run it like this:
_currentTask = Task.Run(GetSomeAction());
I need to ensure it restarts if it throws an exception. At first, I wrote this:
async Task EnsureTaskExecution()
{
try
{
await _currentTask;
}
catch (Exception x)
{
Log.Error($"task exception: {x.GetType()} : {x.Message} {x.StackTrace} {x.InnerException?.GetType()} {x.InnerException?.Message}.");
//...?
}
}
How do I properly restart it though? Inserting this into catch block seems like bad idea, since it introduces recursion:
_currentTask = Task.Run(GetSomeAction());
await EnsureTaskExecution();
Are there any better options? Is there recommended pattern for this? This seems like an almost trivial task, yet I can't find anything.