在asp.net命令行程序,实时显示输出 [英] Display output from command line program realtime in asp.net

查看:180
本文介绍了在asp.net命令行程序,实时显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写那里的应用程序运行使用System.Diagnostics程序类的系统上的命令的web应用程序。
我想从,这需要大量的时间来完成一个命令来显示实时输出。搜索了一下后,我发现BeginOutputReadLine可以流输出到事件处理程序。

I am writing a web app where the application runs a command on the system using System.Diagnostics class. I wanted to display realtime output from a command which takes a lot of time to complete. After searching a bit, I found that BeginOutputReadLine can stream output to an event handler.

我也使用jQuery AJAX调用此方法并运行的过程是异步的。
到目前为止,我试图做这种方式:

I am also using jquery ajax to call this method and have the process run asynchronously. So far, I am trying to do it this way:

过程P2 =新工艺();
p2.OutputDataReceived + =新DataReceivedEventHandler(opHandler);
P2 =的Process.Start(PSI2);结果
p2.BeginOutputReadLine();

我已经声明了一个静态变量的命令的输出存储为页面上的标签一类不会是从静态方法访问。

I have declared a class with a static variable to store the output of the command as a Label on the page wont be accessible from a static method.

public class ProcessOutput
{
    public static string strOutput;

    [WebMethod]
    public static string getOutput()
    {
        return strOutput;
    }
}

在为BeginOutputReadLine事件处理程序,设置与输出线变量。

In the event handler for BeginOutputReadLine, set the variable with the line from output.

private static void opHandler(object sendingProcess,DataReceivedEventArgs outLine)
    {
        if (!String.IsNullOrEmpty(outLine.Data))
        {
            ProcessOutput.strOutput= outLine.Data;  
        }
    }

和从ASPX页面,我打电话的方法来获得strOutput的值

and from the aspx page, I am calling the method to get the value of strOutput

    $(document).ready(function() {

setInterval(function() { 
  $.ajax({
   type: "GET",
   url: "newscan.aspx/getOutput",
   data: "",
   success: function(msg){
     $('#txtAsyncOp').append(msg.d);
   }
 });
}, 1000);

});     

我不知道为什么,但拉布勒是没有得到更新。如果我把警报,我得到在每10秒的警告框未定义。
任何人都可以建议我怎么做是正确的?

I dont know why, but the lable is not getting updated. If I put alert, I get 'undefined' in the alert box every 10 seconds. Can anybody suggest me how to do it correctly?

推荐答案

每个请求开始一个新的线程为请求管道的一部分。这是由设计。每个线程都有自己的堆栈,并不能访问对方的筹码。当一个线程开始运行的新方法它存储在其自己的堆栈方法的参数和局部变量。长话短说,你将无法分配变量,并期望从另一台请求其价值。

Each request begins a new thread as a part of the Request pipeline. This is by design. Each thread has its own stack and can't access each others stacks. When a thread starts running a new method it stores the arguments and local variables in that method on its own stack. Long story short you won't be able to assign that variable and expect to retrieve its value from another Request.

有几个方法可以采取,你可以用范围会话变量(最常见):

There are a couple approaches you can take, you can scope it to the session variable (most common) with:

System.Web.HttpContext.Current.Session["variable"]  = value ; 

或者你用它设置为应用范围:

Or you set it to application scope using:

if (System.Web.Caching.Cache["Key1"] == null)
      System.Web.Caching.Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);

另外,你可以记录输出到一个数据库或文件和回声出通过的WebMethod的结果。如果你的长期运行的进程是异步运行,您将无法访问HttpContext的 - 这样的会话状态包将无法使用;应用程序缓存可以使用,但它一般不用于这种类型的机制(缓存可用于性能方面的原因,而不是一个持久性机制 - 它的重要的是要记住,当你的web应用回收你无法控制)。

Alternatively, you can log the output to a database or file and echo out the results via the WebMethod. If your long running process is running asynchronously, you won't have access to the HttpContext -- so the Session state bag will not be available; the application Cache could be used, however it is generally not used for this type of mechanism (cache is available for performance reasons, not a persistence mechanism -- its important to remember that you cannot control when your web application recycles).

我强烈建议写入数据库或日志文件。异步过程通常需要记录的输出或跟踪来诊断潜在的问题,并验证结果。

I'd highly suggest writing to a database or log file. Asynchronous processes commonly require logged output or trace to diagnose potential problems and to validate results.

此外,因为当Web应用程序回收你无法控制,你可以很容易失去你推出的子进程的控制权。更好的设计会开始一个异步方法过程中的,或出-of流程应用程序或服务轮询数据库拿起作业(可能使用任务调度的cron或者根据您的平台)。

Furthermore, because you cannot control when the web app recycles, you can easily lose control of that child process you're launching. A better design would start an asynchronous method in-process, or an out-of-process application or service that polls a database to pick up jobs (possibly use the task scheduler or cron depending on your platform).

这篇关于在asp.net命令行程序,实时显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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