MVC异步控制器不解!!!!!???
public class PortalController : AsyncController {
public void NewsAsync(string city) { AsyncManager.OutstandingOperations.Increment();
NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
AsyncManager.Parameters["headlines"] = e.Value;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync(city);
} public ActionResult NewsCompleted(string[] headlines) {
return View("News", new ViewStringModel
{
NewsHeadlines = headlines
});
}
}
异步不是会另外开一个线程出来的吗?比如委托的异步BeginInvoke,使用BeginInvoke后,这个独立的线程连当前的httpcontext都是未知的(除非传参数),他又怎么能把异步返回的数据呈线在页面上呢??
比如异步读取新闻也许这个异步操作还没完成,整个http请求就结束了,因为http是无状态无连接的,又怎么将数据返回到页面上呢??
public class PortalController : AsyncController {
public void NewsAsync(string city) { AsyncManager.OutstandingOperations.Increment();
NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
AsyncManager.Parameters["headlines"] = e.Value;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync(city);
} public ActionResult NewsCompleted(string[] headlines) {
return View("News", new ViewStringModel
{
NewsHeadlines = headlines
});
}
}
异步不是会另外开一个线程出来的吗?比如委托的异步BeginInvoke,使用BeginInvoke后,这个独立的线程连当前的httpcontext都是未知的(除非传参数),他又怎么能把异步返回的数据呈线在页面上呢??
比如异步读取新闻也许这个异步操作还没完成,整个http请求就结束了,因为http是无状态无连接的,又怎么将数据返回到页面上呢??
因为http是无状态无连接的,
所以不存在异步没完成,
http请求就结束了这个说法!!好好再理解下无状态无连接的意思吧
我也没这个语言天赋和耐心,打个简单的比方:我到店里买了一张拼图,
拿回家发现少了一块,
于是叫我儿子再去店里把那块缺少的拿回来,
儿子拿回来给我后,
我再把拼图拼完整.我让儿子去拿这件事,
针对我需要一个完整的拼图画---这件整体事件而言,
就相当于是一个AJAX请求.
我发现使用EndInvoke后会阻塞线程,也就是说,要得到结果,就必需要用EndInvoke来返回,但我不明白,使用EndInvoke导致的阻塞,这种异步的方式为什么会节省时间呢?另外这和ajax不一样吧,ajax取得数据后,可以通过dom来改变页面元素,而dom是客户端的东西,当然可以随时改变了,而http是无状态无连接的