views:

255

answers:

1

The following code is working great in a normal console application:

    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; }));
    }

but In a web application, it starts 'a.exe', but dosen't outputs to the textbox. How can I fix it? Thanks.

+1  A: 

You need to remember the difference between web applications and console/WinForms applications. You've got to return a page to the client. At the moment you're saying "tell me when the process writes out a line" but then immediately returning the page... by the time the process has written anything, the web page will already have rendered.

You might want to either wait for the process to exit or at least wait for a few seconds. Bear in mind that the user won't see anything while they're waiting for the page to be returned.

You can do event-like things using techniques like Comet, but that's pretty complicated.

Jon Skeet