开发者

In Javascript, how to start a server-side process and monitor its output until completion?

开发者 https://www.devze.com 2023-04-01 15:34 出处:网络
In my web application I\'d like to be able to start a process with certain parameters on the web 开发者_Python百科server, and continuously display output from the process in a text area until it compl

In my web application I'd like to be able to start a process with certain parameters on the web 开发者_Python百科server, and continuously display output from the process in a text area until it completes. How can I accomplish this with Javascript? I'm using jQuery along with ASP.NET MVC 3.


You can do this with 2 action methods and a javascript timer

[HttpPost]
public JsonResult StartProcess()
{
    StartTheMachine();
    return new JsonResult() { Data = "Started" };
}

[HttpGet]
public JsonResult GetProcessUpdate()
{
    return new JsonResult() 
    {
        Data = GetUpdate(), 
        JsonRequestBehavior = JsonRequestBehavior.AllowGet 
    };
}

and in your view something like this:

$.post("<%=Url.Action("StartProcess") %>", function(data) {
    // do something with data "Started" and start timer
    setTimeout(GetUpdate, 5000);
});

function GetUpdate()
{
    $.get("<%=Url.Action("GetUpdate") %>", function(data) {
        if (data.Complete) // or some way to tell it has finished
        {
            // do something with other data returned
        }
        else
        {
            // call again if not finished
            setTimeout(GetUpdate, 5000);
        }
    });
}


I think you need to use some of the new stuff coming out. For details see:

SignalR according to Scott Hanselman

Node.js according to Scott Hanselman

Having said that, it is all new, and still under development I understand. But it looks like the way this kind of development is going.


you need to send a ajax post(or get) in regular time intervals to server side and get the process status.

$.get("controler/ActionToReturnStatusView", null,
   function(data){
     alert("status " + data);
   });


can use comet programming. can try web workers supported in HTML 5

0

精彩评论

暂无评论...
验证码 换一张
取 消