1

Following scenario:

I have a Console Application with following code:

static void Main(string[] args)
{
      // Some code.
      Console.WriteLine("Done");
      Console.ReadLine();
}

The application is successfully built and "Test.exe" is generated.

Now I have another Console application which executes the generated exe.

static void Main(string[] args)
        {
            var processStartInfo = new ProcessStartInfo("Test.exe")
            {
                UseShellExecute = false,
                RedirectStandardOutput = true
            };

        // Setup the process
        process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };

        process.OutputDataReceived += ProcessOnOutputDataReceived;
        process.Start();
        process.BeginOutputReadLine();
        // Detect here if application is completed.
        }

        Console.ReadLine();
    }

    private static void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
    {
        Console.WriteLine(dataReceivedEventArgs.Data);
    }

When I execute the above code, it calls the "Test.exe" and output of "Test.exe" is displayed in the console window of current process.

"Test.exe" waits for input at "Console.ReadLine()" and the current process waits for "Test.exe" to finish.

Is it possible to detect if "Test.exe" is waiting for an input in current process?

lerner1225
  • 862
  • 7
  • 25
  • duplicate of http://stackoverflow.com/q/40421394/1132334, but that was not answered either – Cee McSharpface Mar 22 '17 at 16:30
  • You could just look for the Done message, yeah? – itsme86 Mar 22 '17 at 16:31
  • 1
    http://stackoverflow.com/a/14510536/1132334: investigating `ThreadWaitReason` in this context may be worth a try – Cee McSharpface Mar 22 '17 at 16:32
  • You need some form of IPC (unless you can't modify child process application code), in simplest case just wait until child process is [finished](http://stackoverflow.com/q/3147911/1997232). – Sinatr Mar 22 '17 at 16:34
  • admitted, the design goal/choice of architecture may be flawed. why would one want to automate two console processes in this way, there are better ways of inter-process communication. but the actual question, *detect if "Test.exe" is waiting for an input*, is intriguing. – Cee McSharpface Mar 22 '17 at 16:35
  • Note also comments to the answer in the duplicate question. – Evk Mar 22 '17 at 16:37

1 Answers1

0

is there a way to synchronously call OutputReadLine()? if yes, then your applicalion will block until test.exe finishes and only then will your next line of code be executed. there would be no need to check.