0

I'm facing a problem with the console portion of my app not waiting for the program to finish before accepting a new command. This is an app that has optional command line run mode with no GUI like many others have described before. I check for certain conditions when running and determine if I should use the GUI or if I should "attach to the console that called the program".

 [DllImport("kernel32.dll")]
 static extern bool AttachConsole(int dwProcessId);
 private const int ATTACH_PARENT_PROCESS = -1;
 if (args.Length > 1) { AttachConsole(ATTACH_PARENT_PROCESS); } 

(I got the gist of this code from Stack Overflow, but could not locate the OP)

So if I Console.Writeline("this string here"), then it will write to the window that started the program what I tell it to.

I would like to stop that window from accepting user input while my program is running, so I can continually feed out to the window without being interrupted.

For example, when you do an ip config command, you cannot make any more commands from that window, until everything has been written to the window.

I'm fairly certain after thorough searching that this is a unique question. Should it be found otherwise please refer me to that question.

**Note, I don't want "Press any key to continue" functionality. I want it to make the user wait until the application finishes then re-enable the console window for user input.

-Thank you kindly!

  • 1
    This isn't very possible. See http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx – SLaks Aug 13 '15 at 16:50
  • 1
    Could you adapt something [like this](http://stackoverflow.com/questions/5147327/disabling-input-in-c-sharp-console-until-certain-task-is-completed)? Looks like `ReadKey(true)` prevents keys from being printed. You might make `bool` variable called `done` and set it to `true` after `AttachConsole`. – TestWell Aug 13 '15 at 16:54
  • Both of you gave informative links thank you! I'm not accepting what I ended up doing as an answer, but when the program runs with command line arguments, it *needs* to wait until it's complete. Because of that, I changed the output type to command line. Then when the program is called and needs the GUI I hide the console that appears using code similar to [this](http://stackoverflow.com/a/3571628/4990978). This does cause flashing of a console window on open and close when running the application. It also takes control of the console window for the duration the GUI is shown. – Celebrating Octopus Emoji Aug 14 '15 at 16:18
  • `Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; };` is solved problem for me.But still if you close the console window it closes whole application. – mbpakalin Oct 24 '19 at 08:23

0 Answers0