U
U
UniverseElement2021-08-31 11:37:47
ASP.NET
UniverseElement, 2021-08-31 11:37:47

What is the difference between ActionResult and Task(ActionResult) and ToList() and ToListAsync()?

Below are two methods that essentially do the same thing, one of them is asynchronous.

[HttpGet]
public ActionResult Users()
{
return View(_userManager.Users.ToList());
}

[HttpGet]
public async Task(ActionResult) UsersAsync()
{
return View(await _userManager.Users.ToListAsync());
}

As I understand it, await _userManager.Users.ToListAsync() is executed on another thread, temporarily freeing the calling thread.

Is there any point in such asynchrony within a web application if, by freeing one thread, we occupy another?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-08-31
@UniverseElement

await _userManager.Users.ToListAsync() is executed on a different thread, temporarily releasing the calling thread.

No. This will be called on the current thread, but when it comes to IO, the thread will be freed. The new thread will not be busy.
Is there any point in such asynchrony within a web application

There is, and even MS themselves recommend it.

A
AndromedaStar, 2021-08-31
@AndromedaStar

Of course there is a difference. But to see this clearly, try to simulate a situation with 10,000 simultaneous users of your application. Google api load testing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question