In the Program.cs file, you probably have a function like this:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
This feature is the entry point for your application. The Application.Run() function starts the main application loop. The main loop of a graphical application is where the event handler fires events, the user interface is updated, and so on. If an event (such as a button click) takes too much time to process, the user interface freezes. To prevent this from happening, you can use threads.
The Application.Run() function is overloaded, so if the function has a parameter ( new Form1() in this case), the form becomes the "main" form, so the main loop will exit when the form is closed.
To fix this problem, you need to remove the parameter that will make the main loop work without closing when the form closes:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(); }
However, this creates 2 problems:
When you start the form is not displayed, because we removed it from the Main function. To fix this, you need to create a new form in the main function and show it:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 form = new Form1(); form.Show(); Application.Run(); }
The application will not exit when you close the form. If you close all forms, the process will still be running, so you need to call Application.Exit(); when you want the application to exit (for example, a form closing event).
source share