3

I am simulating a console window in my project. I use the Process class to run a console application, and I use a TextBox for the user to interact with the console application. I wish to make the TextBox enabled only when the process is waiting for user input.

However I could find no way to do that. Here is a similar question: Is my process waiting for input? But the solution to that question does not work for me. I have tried

foreach (ProcessThread thread in process.Threads)
    if (thread.ThreadState == ThreadState.Wait)
    {
        // see what thread.WaitReason is
    }

But thread.WaitReason always give ThreadWaitReason.Executive, both when the process is busy and when it is waiting for input.

Now my alternative solution is to parse the output, since the console application prints a string of a certain pattern every time it needs an input. But that's not a good solution, since there might be mistakes, and it may fail with other console applications, if the output is very difficult to parse, or not parsable at all.

So, is there a way to detect whether the process is waiting for input?

Community
  • 1
  • 1
Keke Zhang
  • 91
  • 5
  • Why not just run a normal console window? – stuartd Nov 04 '16 at 11:35
  • 1
    There just isn't any point in helping. The user can already tell when it is ready. And if you send input and the app isn't ready then it just gets buffered. Type-ahead, nice feature. – Hans Passant Nov 04 '16 at 11:38
  • I parse and highlight the output with different colors, and I handle the event when the user clicks the lines of the output. It will be difficult to implement if I use just a console window. – Keke Zhang Nov 04 '16 at 11:45
  • @Hans Passant It looks weird when you enter a string and the application gives some output and your input moves along with it. Of course it is okay but I just want to make it more like a real console window, where input is not displayed until the application pauses and waits for input. – Keke Zhang Nov 04 '16 at 11:51
  • FWIW, At a lower level (kernel thread scheduler) windows knows if a thread is waiting on an I/O, but I haven't ever seen a high level API which can provide this information. I have a feeling that the thread scheduler does not retain any information about why a thread was put into wait-state, other than marking them `alertable` or not – Vikhram Nov 04 '16 at 13:03
  • 1
    Possible duplicate of [Is my process waiting for input?](http://stackoverflow.com/questions/1704791/is-my-process-waiting-for-input) – Cee McSharpface Mar 22 '17 at 16:40

0 Answers0