Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

process - C# StandardOutput hangs, how can I detect it's waiting for input?

I have been working with processes (using the Process and ProcessStartInfo) with C# quite some time. Now, there is only one question that really bothered me and still haven't managed to find a way to work it out.

The StandardOutput will hang when the application is "waiting" for some input. Which means, if you are using ReadToEnd it will never return (as the actual input window is invisible).

Now, this creates a problem when I'm using the StandardInput to provide the input. For example:

  1. Starting the process with C#
  2. Entering a command using StandardInput.
  3. Start reading the stream for the ouptut.
  4. Running another one based on the last output.

Now this is the problem: I can't tell whether the process is waiting for input or it's still generating one, as my process "gets" stuck to read from the StandardOutput (it's done on the same thread).

I need to read the output after my command ran, run some algorithms and then run another command based on the previous command output.

Is there is any way of detecting if the process running finished loading my command and waiting for new input? Just like the cmd does?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Trying to follow Peter answer, I haven't found any special delimiter at the end of the StandardOutput (I checked the ErrorOutput as well).

Thinking of this through I thought, why not used echo to "simulate" a delimiter? So I put up this code which allows me to run a specific command and obtain the output immediately on the same thread:

    /// <summary>
    /// A string used to write into the bash and allows us to detect when the bash finished to write it's output.
    /// </summary>
    const string delimiterString = "#WAITING";

    /// <summary>
    /// Runs the specified command and return it's output.
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public string RunCommand(string command)
    {
        sessionProcess.StandardInput.WriteLine(command);

        // Add a special string to be recognized when output is waiting
        sessionProcess.StandardInput.WriteLine("echo "" + delimiterString + """);

        string output = "";
        int lastChr = 0;

        do
        {
            lastChr = sessionProcess.StandardOutput.Read();

            string outputChr = null;
            outputChr += sessionProcess.StandardOutput.CurrentEncoding.GetString(new byte[] { (byte)lastChr });
            output += outputChr;

            if (output.EndsWith(delimiterString))
            {
                // Remove delimeter
                output = output.Replace(Environment.NewLine + delimiterString, "");
                output = output.Replace(delimiterString + Environment.NewLine, "");

                // Console.Write(output);
                break;
            }

        } while (lastChr > 0);

        return output;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...