ASP.NET MVC 4异步控制器回调 [英] ASP.NET MVC 4 Async Controller Callback

查看:155
本文介绍了ASP.NET MVC 4异步控制器回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用在MVC 4新的异步控制器的功能这里描述的http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4

如果我有可能需要10-20秒运行,我想提供某种状态栏通知进度的用户的操作。执行异步功能有什么帮助这个呢?

编辑:我将在我怎么会尝试做一个刺,看看是否有什么更好的办法

 公共异步任务<&的ActionResult GT; GizmosAsync()
{
    返回视图(小玩意儿,等待GetGizmosAsync());
}私人无效GetGizmosAsync()
{
    的for(int i = 0;我小于10;我++)
    {
        锁(_locker)
        {
           _statusMessage =的String.Format({0} 10,I);
        }
        DoSomethingLongRunning();
    }
}公众的ActionResult状态()
{
   返回JSON(新{状态= _statusMessage});
}静态只读对象_locker =新的对象();静态字符串_statusMessage =;....<脚本>的setTimeout(displayStatus,1000);功能displayStatus(){
  $。员额(/控制/状态功能(数据){
    警报(data.Status);
  });
}< / SCRIPT>


解决方案

异步控制器,只是从线程池,以便能够处理大负荷期间传入请求释放的线程在IIS中的机制,但与客户沟通保持为通常的请求 - 响应

状态栏和排序是通常只是JavaScript的在屏幕上显示的东西,直到AJAX请求完成。我不认为MVC4将在该部分援助。

您可以做这样的事情: http://stackoverflow.com/a/68503/1373170 以显示处理......< D​​IV> 中的Ajax调用

修改:如果你需要真正的客户端的进步和交互(如真正的进步),你应该检查出的 SignalR
<一href=\"http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx\">http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx
而这种相关职位:<一href=\"http://stackoverflow.com/questions/8020204/async-controllers-mvc-long-running-process-with-stops\">Async控制器(MVC),长时间运行的进程&QUOT;停止&QUOT;

I am just using the new Async Controller features in MVC 4 as described here http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4

If I have an action that may take 10-20 seconds to run I would like to provide some kind of status bar to notify the user of progress. Do the Async features have anything to help this out?

EDIT: I will take a stab at how I will try and do it and see if there are any better ways

public async Task<ActionResult> GizmosAsync()
{
    return View("Gizmos", await GetGizmosAsync());
}

private void GetGizmosAsync()
{
    for(int i=0; i<10; i++) 
    {
        lock(_locker) 
        {
           _statusMessage = String.Format("{0} of 10", i);
        }  
        DoSomethingLongRunning();
    }
}

public ActionResult Status()
{
   return Json(new { Status = _statusMessage });
}

static readonly object _locker = new object();

static string _statusMessage = "";

....

<script>

setTimeout(displayStatus, 1000);

function displayStatus() {
  $.post("/controller/status", function(data) {
    alert(data.Status);
  });
}

</script>

解决方案

Async controllers is just a mechanism for freeing threads from the ThreadPool in IIS in order to be able to handle incoming requests during heavy load, but the communication with the client remains as the usual request-response.

Status bars and the sort are usually just javascript displaying something on screen until the ajax request finishes. I don't think MVC4 will be of aid in that part.

You could do something like this: http://stackoverflow.com/a/68503/1373170 to display a "processing..." <div> during ajax calls.

EDIT: If you need real client progress and interaction (such as real progress), you should check out SignalR http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx And this related post: Async Controllers (MVC), long running process with "stops"

这篇关于ASP.NET MVC 4异步控制器回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆