Return to View with Async Await

后端 未结 4 1931
故里飘歌
故里飘歌 2020-12-18 06:23

I have a process I would like to run in the background. This is executed with a click of an action link.

Action to call:

   public async Task

        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 06:59

    This however waits for the full 10 seconds before redirecting me to my "Index, Home" action.

    Right, that's because await asynchronously waits for the operations completion. It will yield the thread back to the pool until the operation completes.

    How do I get the application to return to this action, while the waitTimer is executing in the background?

    Task.Run is dangerous in the fact it doesn't register work with IIS which can lead to problems. Instead, you can use BackgroundTaskManager or HangFire which register it's execution with ASP.NET:

    BackgroundTaskManager.Run(() => { waitTimer() };
    return RedirectToAction("Index", "Home");
    

提交回复
热议问题