C#Process <instance> .StandardOutput InvalidOperationException“无法在进程流上混合同步和异步操作。”</instance>

时间:2010-04-21 07:35:42

标签: c# .net process asynchronous

我试过这个

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

然后我收到此错误.. alt text http://img188.imageshack.us/img188/3759/errorstack.jpg

我的过程需要很长时间才能完成,所以我需要在运行时显示进度。

1 个答案:

答案 0 :(得分:3)

您无需调用ReadLine(),读取的文本行是DataReceivedEventArgs对象中传递给您的属性之一。