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

前端 未结 2 1960
不思量自难忘°
不思量自难忘° 2020-12-21 02:57

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 i

相关标签:
2条回答
  • 2020-12-21 03:24

    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);
    
    0 讨论(0)
  • 2020-12-21 03:33

    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();
    }
    
    0 讨论(0)
提交回复
热议问题