Since I code in Visual Studio (2015), there are three main ways to launch my app:
- Pressing F5 in VS ("Start debugging")
- Pressing Ctrl+F5 in VS ("Start without debugging")
- Run the app from outside VS
With methods 1 and 3 the app window closes when the app ends execution. With method 2 it waits for any key before closing.
How do I make it wait for any key in all 3 cases? I use all 3 launching methods.
I could detect if I'm in case 2, and if not, call Console.ReadKey()
. But how to detect it?
I can detect case 1 by checking System.Diagnostics.Debugger.IsAttached
. But I can't differentiate between cases 2 and 3.
I tried the following to differentiate between those two cases but in both cases the result is false
:
Process currentProcess = Process.GetCurrentProcess();
string moduleName = currentProcess.MainModule.ModuleName;
bool isLaunchedFromVS = moduleName.Contains(".vshost");
Note: Another approach would be to call ReadKey
in all cases, but then I would need to know how to disable the built-in keywait of case 2. And according to this question this isn't possible.
Related question:
Check if application was started from within Visual Studio - but it doesn't ask how to differentiate between cases 2 and 3.