0

I'd want to achieve something like this:

Requests comes, start some heavy job, return Ok and it's being processed in the background

I managed to do it in example like this:

[HttpPost]
[Route("/test")]
public IActionResult Test2()
{
    Task.Delay(20_000).ContinueWith(t => Console.WriteLine("completed"));
    Console.WriteLine("return");
    return Ok();
}

but I struggle to apply it to something more "realistic"

[HttpPost]
[Route("/test")]
public IActionResult Test2()
{
    Handler.TestTask();
    Console.WriteLine("returning");
    return Ok();
}

public Task TestTask()
{
    var tasks = new Task[] { Task.Factory.StartNew(() => Thread.Sleep(5000)) };
    Task.WaitAll(tasks);

    Console.WriteLine("tasks ended");
    return Task.CompletedTask;
}

What I'm doing wrong?

Axelly
  • 275
  • 1
  • 7
  • 20

0 Answers0