Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my process waiting for input?

I am using the Process class to run an exe.

The exe is a 3rd party console application that I do not control.

I wish to know whether the process is waiting for input on the command line.

Should it make any difference, I intend to kill the application should it be waiting for input.

There are suitable events for when there is output from the program waiting to be read, but I cannot see anything similar for when the process is waiting patiently for input.

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "myapp.exe";
            info.CreateNoWindow = true;
            info.UseShellExecute = false;
            info.RedirectStandardError = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            process.StartInfo = info;

            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);

            process.Start();

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();


            process.WaitForExit();

How do I detect that my process is waiting for input?

like image 521
Don Vince Avatar asked Nov 10 '09 00:11

Don Vince


People also ask

How do I know if my command prompt is working?

Type Ctrl+Z to suspend the process and then bg to continue it in the background, then type an empty line to the shell so it'll check whether the program got stopped by a signal. If the process is trying to read from the terminal, it will immediately get a SIGTTIN signal and will get suspended.


2 Answers

Depending on what the 3rd party process is doing exactly you could try polling its threads' states:

foreach(ProcessThread thread in process.Threads)
    if (thread.ThreadState == ThreadState.Wait
        && thread.WaitReason == ThreadWaitReason.UserRequest)
            process.Kill();

Failing that... you can try to

process.StandardInput.Close();

after calling Start(), I conjecture that an exception will be raised in the child process if it's trying to read from standard input.

like image 196
Serguei Avatar answered Sep 21 '22 09:09

Serguei


If the console application has some sort of prompt waiting for input, you could periodically parse out the console output text using the Process.StandardOutput property of the process and wait for said prompt. Once the proper string is detected, you know that it's waiting for input. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx.

like image 45
nithins Avatar answered Sep 18 '22 09:09

nithins