Function inside Task.Run doesn't get executed

262 Views Asked by At

I have some slow part of my code that does a http request then waits for the answer and calls an adf pipeline so it can update a database parameter at the end. Since this took 2 minutes and my client app seemed so slow for my clients, I wrapped this call inside Task.Run() and set my logic inside. This works good but the last part, update a database parameter, doesn't get executed at all.

Task.Run(async () =>
{
    await client.ExecuteAsync(request, CancellationToken.None);
    await RunADFPipeline(pipeline, _config);
    await _projectRepository.UpdateValue(id, "status", "unlock");
});

I found this example online and I applied it in my code. The first two awaits seem to execute correctly but the third one doesn't execute at all. If I set it outside of Task.Run(), it executes. The third part literally just calls a stored procedure

return await _context.Database.ExecuteSqlRawAsync("EXEC core.UpdateValue @ProjectId, @Name, @Value", parameters);

I also tried by adding ContinueWith() at the end of the Task.Run() but still no luck.

Task.Run(async () =>
{
    await client.ExecuteAsync(request, CancellationToken.None);
    await _adfHelper.RunADFPipeline(pipeline, _config);
}).ContinueWith((result) => _projectRepository.UpdateValue(id, "status", "unlock"));

All this code is inside a controller. Any thoughts and ideas on how can I achieve my goal and update this parameter after Task.Run() finishes or the functions inside finish?

0

There are 0 best solutions below