本文介绍了ASP.NET MVC4异步控制器 - 为什么使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么当我应该使用异步控制器操作。
最终,当我使用它等待,它会等待操作,以返回视图来完成的。

I am trying to understand why and when should I use an async controller action.Eventually, when I'll uses "await" in it, it will wait for the operation to complete in order to return the View.

例如

public async Task<ActionResult> TryMe()
{
   await SomeActionAsync();
   return View();
}

在这种情况下,如果我使用异步或不使用异步,行动将采取相同的时间执行。

In this case if I use the async or not using the async, the Action will take the same time to execute.

如果我不尝试运行至少2个缓慢的操作(即不依赖于对方)并行,我看不出有任何理由使用异步控制器动作。

If I am not trying to run at least 2 slow operations (that are not dependent on each other) in parallel, I don't see any reason to use an async controller action.

请纠正我,如果我错了。我想我在这里失去了一些东西。

Please correct me if I'm wrong. I think I'm missing something here.

推荐答案

等待关键字的一点就是让你的的异步操作的工作无需编写丑陋的回调。

The point of the await keyword is to let you work with asynchronous operations without writing ugly callbacks.

使用异步操作有助于避免浪费线程池中的线程。

Using asynchronous operations helps avoid wasting thread pool threads.

ASP.Net运行所有code在从管理线程池中的线程。结果
如果你有太多的缓慢请求同时运行,线程池将得到充分和新的要求,需要等待一个线程来获得免费的。

ASP.Net runs all of your code in threads from the managed thread pool.
If you have too many slow requests running at once, the thread pool will get full, and new requests will need to wait for a thread to get free.

通常情况下,但是,您的要求是缓慢的,不是因为他们正在做计算(计算密集型),而是因为他们在等待其他的东西,如硬盘,数据库服务器,或外部Web服务(IO - 或网络绑定)

Frequently, however, your requests are slow not because they're doing computation (compute-bound), but because they're waiting for something else, such as a hard disk, a database server, or an external webservice (IO- or network-bound).

有在浪费了precious线程池线程没有一点只是等待外部操作来完成。

There is no point in wasting a precious threadpool thread simply to wait for the external operation to finish.

异步操作让你开始操作,你的线程返回到池中,然后当操作完成不同的线程池线程唤醒。结果
而操作运行时,没有线程消耗

Asynchronous operations allow you to start the operation, return your thread to the pool, then "wake up" on a different thread pool thread when the operation is finished.
While the operation is running, no threads are consumed.

这篇关于ASP.NET MVC4异步控制器 - 为什么使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:46