0

Before start let me say that I already tried to use variant which is described there.

Problem is folowing, I have very simple program in C which is just write to output and read input.

#include <stdio.h>
#include <stdint.h>

int main()
{
    int size;

    printf("Enter size: ");
    scanf("%d", &size);
    printf("\n size = %d", size);

    return 0;
}

Second program wrote in C#. This program should start from another process first and obtain input and output. But the program is freezed when try to read input. At first call of Console.Write(process.StandardOutput.ReadLine());

static void Main(string[] args)
{
    try
    {
        var fName = "test.exe";
        var pci = new ProcessStartInfo
        {
            FileName = fName,
            Arguments = "/c DIR",
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
        };

        using (var process = Process.Start(pci))
        {
            Console.WriteLine(process.ProcessName);

            Console.Write(process.StandardOutput.ReadLine());
            process.StandardInput.WriteLine("4");
            Console.Write(process.StandardOutput.ReadLine());

            process.WaitForExit(1000);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    Console.ReadLine();
}

Does anybody know why it freezed? Or where to take a look?

P.S. with asnyc reading problem the same

  • The isue is process.WaitForExit(1000); So you are waiting for the Process to terminate. Does the application automatically close when finish? Is application putting a '\n' at the end of each response. ReadLine() expects a '\n'. – jdweng Apr 10 '20 at 17:03
  • Write the input *before* trying to read the output. – Hans Passant Apr 10 '20 at 17:04
  • Regarding rocess.WaitForExit(1000) Code even not reach this line. In debugger it stuck on line just after 'Console.WriteLine(process.ProcessName);'. ReadLine - it was last version. Before I have used just Read, of cycle as used in Microsoft documentation. Result the same – Egor Plotkin Apr 10 '20 at 17:15
  • 'Write the input before trying to read the output' Why should be before? As you may see that the first line also contains information which I expect to get. – Egor Plotkin Apr 10 '20 at 17:24
  • As @jdweng already pointed out: ReadLine expects an output *line*, including a newline. `printf("Enter size: ");` does not write a newline. – Klaus Gütter Apr 10 '20 at 19:18
  • @KlausGütter, thank you, but it still not works. I added in C program at the end of each printf linefeed. Still no result – Egor Plotkin Apr 11 '20 at 11:12
  • Sounds like an exact duplicate of https://stackoverflow.com/q/11292917/3034273 . Can you update your question where you've added linefeed if that still doesn't work? – Xerillio Apr 11 '20 at 11:17
  • @Xerillio, you are right! was need just add `fflush(stdout);`. Thank you so much – Egor Plotkin Apr 11 '20 at 11:54

0 Answers0