0

I'm working on a simple console application and I'm attempting to read an user input during process WaitForExit().

var process = Process.GetCurrentProcess();
while (process.WaitForExit(6000)) arg1 = Console.ReadLine();

arg1 is defined as an empty string. The problem is that my WaitForExit executes but anything inside the loop does not, so the program waits for 6 seconds and proceeds without asking for input.

I also tried something like:

process.WaitForExit(6000);
while (!process.hasExited) arg1 = Console.ReadLine();

Is there anyway to do this by using WaitForExit() ?

xc3llion
  • 11
  • 2
  • Take a look at the answer here: http://stackoverflow.com/questions/9213569/waitforexit-doesnt-work-correctly – awh112 Feb 01 '17 at 14:37
  • 1
    You can "Wait for" console input or you can "Wait for" the process to exit. which do you want to wait for first, you can't have both. Also calling `WaitForExit` on a process you got from `Process.GetCurrentProcess()` does not make any sense. If the current processed exited then your code is not running. You effectively just have a `Thread.Sleep(6000); while (false) arg1 = Console.ReadLine();` – Scott Chamberlain Feb 01 '17 at 14:48
  • Hard to guess what the intention might be, but consider testing for Console.KeyAvailable instead. – Hans Passant Feb 01 '17 at 15:54

0 Answers0