I'm not going to get into the details of whether or not you should actually do what you want. At first it seems like a bad practice. But considering you have reasons to do this...
When your process closes, whatever it's executing halts automatically. In order to prevent this behavior, you have two options:
Option 1 - Running a second process
Instead of creating one C# project, you create two. The main one uses Process.Start to activate the second one. If the main one closes, the second one will remain executing until it finishes.
Option 2 - Disable the close button
If you don't mind to interact with native Windows code, thus preventing your code from executing in other environments which is now officially going to be supported with VS 2015, you can manually disable the close button from the CMD doing this:
[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)));
}
Reference: https://social.msdn.microsoft.com/forums/vstudio/en-us/545f1768-8038-4f7a-9177-060913d6872f/disable-close-button-in-console-application-in-c
ePoint.Invoke(null, null)in a non-background-thread. if you close the mainWindow your process should still be running (invisible) – Benj Jun 19 '15 at 22:22