Problems getting desired output from Process.Start()

后端 未结 2 1785
忘了有多久
忘了有多久 2021-01-25 18:47

I am working on an application that calls several command line applications to do some post processing on some video files.

Right now I am trying to use Comskip to ide

相关标签:
2条回答
  • 2021-01-25 19:36

    You must set following:

         process.StartInfo.RedirectStandardOutput = true;
         process.StartInfo.RedirectStandardError = true;
         process.StartInfo.UseShellExecute = false;
         process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
         process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutput);
    
         process.Start();
         process.BeginOutputReadLine();
         process.BeginErrorReadLine();
         process.WaitForExit();
    

    and catch the output in ReadOutput and ErrorOutput

      private static void ErrorOutput(object sender, DataReceivedEventArgs e)
      {
         if (e.Data != null)
         {
            stdout = "Error: " + e.Data;
         }
      }
    
      private static void ReadOutput(object sender, DataReceivedEventArgs e)
      {
         if (e.Data != null)
         {
            stdout = e.Data;
         }
      }
    
    0 讨论(0)
  • 2021-01-25 19:38

    See the docs on RedirectStandardOutput. Waiting for the child process to end before reading the output can cause a hang.

    It particular, the example says not to do what you have done:

     Process p = new Process();
     // Redirect the output stream of the child process.
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.FileName = "Write500Lines.exe";
     p.Start();
     // Do not wait for the child process to exit before
     // reading to the end of its redirected stream.
     // p.WaitForExit();
     // Read the output stream first and then wait.
     string output = p.StandardOutput.ReadToEnd();
     p.WaitForExit();
    

    You should use the events OutputDataReceived and possibly ErrorDataReceived and update the progress bar in the handler.

    0 讨论(0)
提交回复
热议问题