question

Dondon510-9757 avatar image
0 Votes"
Dondon510-9757 asked Bruce-SqlWork commented

How to wait all tasks to finished before returning to a view?


I have codes below, but looks like it returns to the view before all of the async tasks have been finished.

How to wait all tasks to finished before returning to a view?

 public async Task<IActionResult> Learning01()
 {
     JObject jo1 = await DoSales_Store1Async(param1);
     JObject jo2 = await DoSales_Store2Async(param1);
     JObject jo3 = await DoSales_Store3Async(param1);
    
     JObject joData = JObject.FromObject(new
     {
        jo1 = jo1,
        jo2 = jo2,
        jo3 = jo3
     }
    
     return await Task.FromResult(Json(joData));
 }
dotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have codes below, but looks like it returns to the view before all of the async tasks have been finished.

No. Each of the tasks has an await.

How to wait all tasks to finished before returning to a view?

The code does not return a View, it returns Json. If the Json results are unexpected then there are issues elsewhere in your code. Troubleshoot your code to find the issue. If you need community debugging support then share all the relevant code, the steps to reproduce the problem, the expected results, and the actual results.





0 Votes 0 ·
Dondon510-9757 avatar image
0 Votes"
Dondon510-9757 answered

@AgaveJoe
yes, you are right!, I missed something there! my bad.
thank you

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

Also line 14 is just useless code. s/b:

return Json(joData);

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

surferonwww avatar image
0 Votes"
surferonwww answered Bruce-SqlWork commented

It seems to me that async / wait used in your does not make sense. I suggest that you simply write it as follows:

 public IActionResult Learning01()
 {
     // CPU-bound tasks     
        
     return Json(joData);
 }

The reasons are:

Asynchronous Code Is Not a Silver Bullet
https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/october/async-programming-introduction-to-async-await-on-asp-net#asynchronous-code-is-not-a-silver-bullet

"Async and await on ASP.NET are all about I/O. They really excel at reading and writing files, database records, and REST APIs. However, they’re not good for CPU-bound tasks. You can kick off some background work by awaiting Task.Run, but there’s no point in doing so. In fact, that will actually hurt your scalability by interfering with the ASP.NET thread pool heuristics. If you have CPU-bound work to do on ASP.NET, your best bet is to just execute it directly on the request thread. As a general rule, don’t queue work to the thread pool on ASP.NET."


Avoid blocking calls
https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-6.0#avoid-blocking-calls

"ASP.NET Core already runs app code on normal Thread Pool threads, so calling Task.Run only results in extra unnecessary Thread Pool scheduling."


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is not good advice for asp.net core. Asp.net core has a fixed number of request processing threads. Any high cpu tasks will block scaling. You should always run high cpu tasks on a new thread. The await syntax makes this easy.

0 Votes 0 ·