Turn from: http://blog.csdn.net/allenjy123/article/details/7727500
First of all, Program.cs
[CSharp] View Plain copy [stathread] static void main () { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (False); // The automatically generated code is like this // application.run (New form1 ()); // Express Instantiate a new Form1 and show at this time the program goes into message loop // once the Form1 is closed, the program closes. // in order to get the program Form1 can continue operation after shutdown need to be modified under new form1 (). Show (); application.run (); / / do this to avoid Form1 shutdown after the program automatically exits // But what is the risk of doing so? // Once the user forgets the application.exit (); // The program is closed after all Windows Its process still has no end // so application.exit (); This line of code is needed manually added }
Form1:button_click[CSharp]View plain copy Form2 f = new Form2 (); F.show (); This. Close ();
Form2:button_click
[CSharp] view plain copy Form1 f = new Form1 (); F.show (); This. Close ();
Note: Call Application.exit () when the last window is closed; Otherwise, the program process is not going to end.
Workaround: (assuming Form2 is the last window)
[CSharp] View plain copy Private void panel1_click (Object sender, eventargs e) { Form2 f2 = new Form2 (); f2. Closed += new eventhandler (this.f2_closed); //f2. closed += (Obj, args) => { application.exit (); }; f2. Show (); this. Close (); } private void f2_closed (object sender, eventargs e) { application.exit (); } The method you have actually used
In C # WinForm, 2 forms, Form1 and Form2, perform this when switching to each other
. Hide ();
Form2 Form2 = new Form2 ();
Form2. Show (); At this point you will find that when the Form2 appears, the Form1 is hidden, but when you close Form2, the process is not closed. That means the resource is not released. I looked up a lot of information on the Internet, many did not solve, through my experiment I found:
as long as the main window is closed, all the resources will be released, but when the window is closed (FORM2), the resources are not released, at this time can be in the Form2_formclosed (object sender, Formclosedeventargs e) Add the following code to the event: Application.exit ();//notifies all messages to terminate, closes all forms after termination, and frees resources. The problem is solved.