【问题标题】:process.startinfo.RedirectStandardOutput gives exception and process.waitforexit() hangs C# applicationprocess.startinfo.RedirectStandardOutput 给出异常并且 process.waitforexit() 挂起 C# 应用程序
【发布时间】:2012-09-14 13:14:40
【问题描述】:

使用进程我在使用 process.StartInfo.RedirectStandardOutput = true 时遇到异常; 例外如下 “StandardOut 尚未重定向或进程尚未开始。”

我正在使用 process.WaitForExit 挂起我的应用程序 gui 所以我使用了 process.StandardOutput.ReadToEnd();根据 MSDN,在 WaitForExit 之前,但现在我的进程运行了无限时间,并在日志文件中给出了上述异常,并且 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();

我没有得到我缺少的地方或代码中的错误在哪里...即使我也尝试过在 waitforexit 中超时但没有成功。

【问题讨论】:

标签: c# process processstartinfo waitforexit


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    process.WaitForExit(); 字符串输出 = process.StandardOutput.ReadToEnd();

    你应该试试这个。 更改代码顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2019-06-08
      相关资源
      最近更新 更多