Rick2047
Rick2047

Reputation: 1595

C# Process <instance>.StandardOutput InvalidOperationException "Cannot mix synchronous and asynchronous operation on process stream."

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 http://img188.imageshack.us/img188/3759/errorstack.jpg

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

Upvotes: 2

Views: 3823

Answers (1)

Dean Harding
Dean Harding

Reputation: 72658

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.

Upvotes: 3

Related Questions