1

I have the following application in C#. What I was expecting was that it would open calc.exe, wait for me to close it manually (console would hang), and then it would finally exit. But it opens calc.exe and exits (while calc is still open). What am I missing?

class Program
{
     static void Main(string[] args)
     {
         Process p2 = new Process();
         p2.StartInfo.UseShellExecute = false;
         p2.StartInfo.RedirectStandardError = true;
         p2.StartInfo.FileName = "calc.exe";
         p2.Start();
         string error = p2.StandardError.ReadToEnd();
         p2.WaitForExit();
         Console.WriteLine("done");
    }
}

code copied from here.

Adriano_Pinaffo
  • 1,429
  • 4
  • 23
  • 46
  • Have you tried using async/ await rather than wait for exit? – B Minster Jun 15 '18 at 21:00
  • 3
    What might be happening - just a guess - is that `calc.exe` is re-launching itself or a different executable at startup. For example, imagine you wrote a program that, if executed with no command line arguments, simply executes itself with some default arguments and exits. In such a case the originally spawned process would be observed to exit immediately. – TypeIA Jun 15 '18 at 21:02
  • 1
    I ran this code as is, and shows the expected behavior. Can't reproduce your problem. – Sach Jun 15 '18 at 21:19
  • I tried it too, same results as @Sach (Windows 10 64-bit, .NET 4.6 in VS2017). – TypeIA Jun 15 '18 at 21:21
  • Suggests a possible problem with the project settings. Try pasting this code into a whole new Console app and see. – Sach Jun 15 '18 at 21:24
  • 2
    What is your OS? Recent versions of Windows 10 does not provide native calculator anymore. `calc.exe` is just launch "Metro" calculator and exit. – user4003407 Jun 15 '18 at 22:02
  • good point guys. I'm on a W10, so if the calc.exe is just a caller for some other fancy stuff (like those new Windows 10 windows), that could be the reason my application is closing. I will try it starting other executables and get back to you. – Adriano_Pinaffo Jun 16 '18 at 02:43
  • That was it @PetSerAl. The problem is that calc.exe is that caller to the actual calculator program in Windows 10. When I tried with notepad.exe it worked fine. Thank you. If you change your comment to an answer I can accept it. – Adriano_Pinaffo Jun 19 '18 at 17:14

0 Answers0