Why is Process.StandardInput.WriteLine not supplyi

2019-02-24 13:38发布

问题:

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.

回答1:

I think the problem is in your C program, not in your C# program. When you produce your output, you do not put \n 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\n", result);
fflush(stdout);


回答2:

Your C# code seems to be just fine, I tried it with another C# program:

static void Main(string[] args)
{
    String line = Console.ReadLine();
    Console.WriteLine("I received " + line);
}

And the following code outputs "I received Hello world!". So the problem must be in your C code. As dasblinkenlight already mentioned, new line symbols might be missing.

static void Main(string[] args)
{
    Process cmd = new Process();

    ProcessStartInfo info = new ProcessStartInfo(@"AnotherApp.exe", "");
    info.WorkingDirectory = @"path\to\folder";
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;
    info.UseShellExecute = false;
    cmd.StartInfo = info;
    cmd.Start();

    cmd.StandardInput.WriteLine("Hello world!");

    String output = cmd.StandardOutput.ReadLine();
    Console.WriteLine(output);

    Console.ReadKey();
}


标签: c# c input process