I will not go into the details of whether you really should do what you want. At first, this seems like bad practice. But given that you have reasons to do this ...
When your process closes, everything that it automatically executes stops. To prevent this behavior, you have two options:
Option 1 - Perform the second process
Instead of creating one C # project, you create two. Mainly used by Process.Start to activate the second. If the main one closes, the second will be executed until it ends.
Option 2 - Disable the close button
If you do not mind interacting with your own Windows code, thereby preventing code from running in other environments that are now officially supported by VS 2015, you can manually disable the close button from CMD by following these steps:
[DllImport("user32.dll")] static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); [DllImport("user32.dll")] static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); internal const UInt32 SC_CLOSE = 0xF060; internal const UInt32 MF_ENABLED = 0x00000000; internal const UInt32 MF_GRAYED = 0x00000001; internal const UInt32 MF_DISABLED = 0x00000002; internal const uint MF_BYCOMMAND = 0x00000000; static void Main(string[] args) { EnableCloseButton(this, false); } public static void EnableCloseButton(IWin32Window window, bool bEnabled) { IntPtr hSystemMenu = GetSystemMenu(window.Handle, false); EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED))); }
Link: https://social.msdn.microsoft.com/forums/vstudio/en-us/545f1768-8038-4f7a-9177-060913d6872f/disable-close-button-in-console-application-in-c
source share