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
715 views
in Technique[技术] by (71.8m points)

c# - Why is Process.StandardInput.WriteLine not supplying input to my process?

this is my first question here so I'll be as detailed as I can. I am currently working on a C# program (We'll call it TestProgram) that tests a different program written in C (which I'll refer to as StringGen). TestProgram is supposed to run StringGen in a command window, then feed it a set of input strings and record the output for each one. When StringGen is run, it starts a while loop which waits for an input, submits that input to the processing function, then returns the result.

My problem comes from when I try and have TestProgram submit a string to StringGen. I'm starting StringGen as a process and attempting to feed it input with Process.StandardInput.WriteLine(), then looking for output with Process.StandardOutput.ReadLine(). Before I elaborate further, I'll supply some code.

Here is the main function for StringGen:

int main() {
    char result[255];
    char input[255];

    do {
        fgets(input, 100, stdin);
        result = GetDevices(input); //Returns the string required
        printf("%s", result);
    } while (input != "quit");

    return 0;
}

Here is the C# code where I define StringGen as a process:

Process cmd = new Process();

ProcessStartInfo info = new ProcessStartInfo(command, arguements); // Command is the path to the C executeable for StringGen
info.WorkingDirectory = workingDirectory; // Working Directory is the directory where Command is stored
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
cmd.StartInfo = info;
cmd.Start();

Then I go on to use this process as so:

using (var cmd)
        {
            // Loop through the input strings
            String response;

            foreach (exampleString in StringSet) // Loops through each string
            {
                cmd.StandardInput.WriteLine(exampleString.text); // This is the problem line
                response = cmd.StandardOutput.ReadLine(); // System comes to a halt here
                cmd.StandardOutput.Close();
                if (response == "Something")
                {
                     // Do this
                }
                else
                {
                     // Do that
                }
            }
       }

The WriteLine command does not seem to give any input to StringGen, and so the system hangs at ReadLine because StringGen is not giving any output back. I've tried running StringGen at command line and it works fine and takes input from keyboard and outputs the correct strings back. I've tried everything I can think of and searched all over this site and others trying to find a solution but every example of this kind of code seems to be work fine for everyone else. I can't see what I'm doing wrong. If anyone could suggest a way that I can submit input to my StringGen program from TestProgram I would be really grateful. If I have left anything important out or if anything is unclear, please let me know.

Notes: I have tried both scanf and fgets in StringGen, both produce the same result.

I have tried using a literal string with WriteLine(), but still no input.

I have tried useing Write() and Flush() in TestProgram but to no avail.

I have tried to Close() the input buffer to force a flush, but this has no effect either.

I am not overly familiar with C# as I am editing someone elses code to perform tests on StringGen.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I think the problem is in your C program, not in your C# program. When you produce your output, you do not put at the end. Hence StandardOutput.ReadLine() will wait forever, because there is no end-of-line marker in the stream.

Since the output of your C program is used to synchronize the steps of your co-operating programs, it is also a very good idea to flush it to the output before waiting for the next portion of input:

printf("%s
", result);
fflush(stdout);

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

1.4m articles

1.4m replys

5 comments

57.0k users

...