Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...