Prevent closing the program when closing the console window

So what happens, my console program opens, then runs an external C # program / code from a text file. All this works fine, but when I close the window of the original form, the program / code that it executes also closes. Is there any way to prevent the code of a text file that I ran after closing?

This is the code to call it to run the program.

static void MemExe(byte[] buffer) { Assembly asm = Assembly.Load(buffer); if (asm.EntryPoint == null) throw new ApplicationException("No entry point found!"); MethodInfo ePoint = asm.EntryPoint; ePoint.Invoke(null, null); } 

where the buffer is the code / program in bytes

and this is my main

 static void Main(string[] args) { var data = File.ReadAllText(@"program.txt"); MemExe(data); } 
+5
source share
2 answers

A workaround would be to change the Output type project in Project Properties -> Application -> Output type from the Console application to the Windows application (see screenshot)

How to Change from Console Application to Windows Application

Therefore, the console window is not created, therefore, the process will not be displayed as two running processes and cannot be completed by closing the console window.


This is the approach I would take. Your method runs in a non-background thread, which prevents the process from terminating after the main thread terminates. However, you cannot close the console window. Therefore, I would suggest switching to Windows Application

 using System; using System.IO; using System.Reflection; using System.Threading; namespace StackOverflow { public static class Program { static void Main(string[] args) { RunExternalFunctionThread t = new RunExternalFunctionThread(File.ReadAllBytes(@"program.txt")); t.Run(); } private class RunExternalFunctionThread { private Byte[] code; public RunExternalFunctionThread(Byte[] code) { this.code = code; } public void Run() { Thread t = new Thread(new ThreadStart(this.RunImpl)); t.IsBackground = false; t.Priority = ThreadPriority.Normal; t.SetApartmentState(ApartmentState.STA); t.Start(); } private void RunImpl() { Assembly asm = Assembly.Load(this.code); if (asm.EntryPoint == null) throw new ApplicationException("No entry point found!"); MethodInfo ePoint = asm.EntryPoint; ePoint.Invoke(null, null); } } } } 
+1
source

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

+3
source

All Articles