2009-08-22 77 views
1

下面的代码是一个正常的控制台应用程序工作的伟大:为什么Process.OutputDataReceived在ASP.NET中不起作用,我该如何解决它?

private void button1_Click(object sender, EventArgs e) 
    { 
     Process process = new Process(); 
     process.StartInfo.FileName = @"a.exe"; 

     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardInput = true; 
     process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.UseShellExecute = false; 

     process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived); 

     process.Start(); 
     process.BeginOutputReadLine(); 
    } 

    void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     this.Invoke(new Action(delegate() { textBox2.Text += "\r\n" + e.Data; })); 
    } 

,但在Web应用程序,它启动“a.exe的”,但这么想的输出到文本框。我该如何解决它?谢谢。

回答

1

您需要记住Web应用程序和控制台/ WinForms应用程序之间的区别。你必须返回一个页面给客户端。目前你在说“当流程写出一行时告诉我”,然后立即返回页面......在流程写入任何内容之前,网页已经呈现。

您可能希望等待进程退出或至少等待几秒钟。请记住,用户在等待页面返回时不会看到任何内容。

可以使用像Comet这样的技术做类似事件的事情,但这很复杂。

相关问题