process.standardoutput.ReadToEnd() always empty?

前端 未结 4 808
你的背包
你的背包 2020-12-10 04:14

I\'m starting a console application, but when I redirect the standard output I always get nothing!

When I don\'t redirect it, and set CreateNoWindow to

相关标签:
4条回答
  • 2020-12-10 04:30

    Get rid of the loop and move the call to ReadToEnd to cproc_Exited.

    0 讨论(0)
  • 2020-12-10 04:39

    You have redirection of standard out disabled. Try changing

    cproc.StartInfo.RedirectStandardOutput = false;
    

    into

    cproc.StartInfo.RedirectStandardOutput = true;
    

    Does the following sample from MSDN work for you?

    // Start the child process.
    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();
    
    0 讨论(0)
  • 2020-12-10 04:40

    Why are you looping? Once it's read to the end, it's not going to be able to read any more data, is it?

    Are you sure the text is actually being written to StandardOutput rather than StandardError?

    (And yes, obviously you want to set RedirectStandardOutput to true rather than false. I assumed that was just a case of you copying the wrong version of your code.)

    EDIT: As I've advised in the comments, you should read from standard output and standard error in separate threads. Do not wait until the process has exited - this can end up with a deadlock, where you're waiting for the process to exit, but the process is blocking trying to write to stderr/stdout because you haven't read from the buffer.

    Alternatively you can subscribe to the OutputDataReceived and ErrorDataReceived events, to avoid using extra threads.

    0 讨论(0)
  • 2020-12-10 04:41

    Best way for this is to redirect the output and wait for the events:

        // not sure if all this flags are needed
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.ErrorDialog = false;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.EnableRaisingEvents = true;
        process.OutputDataReceived += process_OutputDataReceived;
        process.ErrorDataReceived += process_ErrorDataReceived;
        process.Exited += process_Exited;
        process.Start();
    
        void process_Exited(object sender, System.EventArgs e)
        {
            // do something when process terminates;
        }
    
        void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            // a line is writen to the out stream. you can use it like:
            string s = e.Data;
        }
    
        void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            // a line is writen to the out stream. you can use it like:
            string s = e.Data;
        }
    
    0 讨论(0)
提交回复
热议问题