Answer the question
In order to leave comments, you need to log in
Who can chew asynchronous controllers?
Good time!
public class HomeController : Controller
{
BookContext db = new BookContext();
public ActionResult Index()
{
IEnumerable<Book> books = db.Books;
ViewBag.Books = books;
return View();
}
// асинхронный метод
public async Task<ActionResult> BookList()
{
IEnumerable<Book> books = await db.Books.ToListAsync();
ViewBag.Books = books;
return View("Index");
}
}
Answer the question
In order to leave comments, you need to log in
await
does not yet mean the creation of an additional thread (thread). Especially when it comes to asynchronous I/O.
In desktop applications, the main thread has the Main Loop (event loop), which, in its free time from handlers, grinds the window's message queue (pulls these same handlers in response to appropriate events, such as button presses). If the handler is too bold, then the messages are not processed - the window stops responding to messages.
On the web (ASP.NET), each new request is allocated a new thread. If requests are too slow, and clients come and go, then there is a risk of exhausting the thread pool. As a rule, the slowest actions are I/O, in which the thread of our program is not busy with anything other than waiting. But operating systems can do asynchronous I/O (signal when data has been read/written), so why don't we return an idle thread to the thread pool? And when the data is considered, we will select the stream and return to the task.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question