简体   繁体   English

process.startinfo.RedirectStandardOutput给出异常并且process.waitforexit()挂起C#应用程序

[英]process.startinfo.RedirectStandardOutput gives exception and process.waitforexit() hangs C# application

using process i am getting an exception while using process.StartInfo.RedirectStandardOutput = true; using process我在使用process.StartInfo.RedirectStandardOutput = true时遇到异常; the exception is below "StandardOut has not been redirected or the process hasn't started yet." 例外情况是“StandardOut尚未重定向或进程尚未开始”。

i am using process.WaitForExit which hangs my application gui so i used process.StandardOutput.ReadToEnd(); 我正在使用process.WaitForExit挂起我的应用程序gui所以我使用了process.StandardOutput.ReadToEnd(); before WaitForExit as per MSDN but now my process run for infinite time and gives above exception in log file and GUI also hangs. 在WaitForExit之前,根据MSDN,但现在我的进程运行无限时间并在日志文件中提供上述异常并且GUI也挂起。

                Process process = new Process();
                process.StartInfo.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);


                string strArguments = ""; 

                process.StartInfo.FileName = @"appname.bat";
                process.StartInfo.Arguments = strArguments;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;

                process.Start();


                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                process.Close();

I am not getting where am i lacking or where is the error in the code... Even i have tried time out in waitforexit also but didn't get success. 我没有得到我缺乏的地方或代码中的错误...即使我已经尝试了waitforexit的时间,但没有取得成功。

Try this 尝试这个

 process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
    process.Start();
    process.BeginOutputReadLine();

process.WaitForExit(); process.WaitForExit(); string output = process.StandardOutput.ReadToEnd(); string output = process.StandardOutput.ReadToEnd();

You should try this. 你应该试试这个。 Change sequence of code. 更改代码序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM