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

发布于 2024-08-29 19:58:35 字数 1243 浏览 2 评论 0原文

我尝试了这个

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

然后我收到这个错误.. 替代文本 http://img188.imageshack.us/img188/3759/errorstack.jpg< /a>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-09-05 19:58:35

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文