views:

108

answers:

1

I tried this

        myProcess = new Process();

        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        myProcess.StartInfo.FileName = "Hello.exe";

        myProcess.StartInfo.Arguments ="-say Hello";
        myProcess.StartInfo.UseShellExecute = false;  

        myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
        myProcess.ErrorDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
        myProcess.Exited += new EventHandler(myProcess_Exited);
        myProcess.EnableRaisingEvents = true;

        myProcess.StartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo.RedirectStandardError = true;
        myProcess.StartInfo.ErrorDialog = true;
        myProcess.StartInfo.WorkingDirectory = "D:\\Program Files\\Hello";

        myProcess.Start();

        myProcess.BeginOutputReadLine();
        myProcess.BeginErrorReadLine();

Then I am getting this error.. alt text

My process takes very long to complete, so I need to show progress in runtime.

+1  A: 

You don't need to call ReadLine(), the line of text that was read is one of the properties passed to you in the DataReceivedEventArgs object.

Dean Harding
I tried this but still the same:void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { StreamReader myStreamReader = myProcess.StandardOutput; String str = myStreamReader.ReadLine(); textBox_StdOutPut.Text += str; }
Rahul2047
@Rahul2047: Sorry, but by the code you put in your previous comment it doesn't look like you did as codeka suggested. You access the current line being issued by the process as it goes by using the value of the DataReceivedEventArgs.Data property: myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { textBox_StdOutPut.Text += e.Data + Environment.NewLine; }
Christian.K
Thanks a lot. Now it works. Thanks.
Rahul2047