0

using process i am getting an exception while using process.StartInfo.RedirectStandardOutput = true; the exception is below "StandardOut has not been redirected or the process hasn't started yet."

i am using process.WaitForExit which hangs my application gui so i used 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.

                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.

Kanvas
  • 165
  • 2
  • 7
  • 23

2 Answers2

2

Try this

 process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
    process.Start();
    process.BeginOutputReadLine();
ígor
  • 1,144
  • 7
  • 16
0

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

You should try this. Change sequence of code.