This is the behavior of modal forms. When you use form.ShowDialog() , you ask for it. The reason for this is that form.ShowDialog does not return until the form is hidden or destroyed. Therefore, when the form is hidden, the pump inside the .ShowDialog form destroys it so that it can return.
If you want to show and hide the form, then you should use the Modelless dialog box model http://msdn.microsoft.com/en-us/library/39wcs2dh(VS.80).aspx
form.Show() returns immediately, you can show and hide this window that you want, and it will not be destroyed until you explicitly destroy it.
When you use non-modal forms that are not child modules, you also need to start the message pump using Application.Run or Application.DoEvents in a loop. If the stream that creates the form terminates, the form will be destroyed. If this thread does not start the pump, then the forms that it owns will be immune.
Edit: this looks like ApplicationContext is for the solution. http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx
Basically, you get a class from ApplicationContext, pass an instance of your ApplicationContext as an argument to Application.Run()
// Create the MyApplicationContext, that derives from ApplicationContext, // that manages when the application should exit. MyApplicationContext context = new MyApplicationContext(); // Run the application with the specific context. Application.Run(context);
The context of your application should know when it is suitable for exiting the application, and when hidden forms should not exit the application. When it's time to exit the application. The application context or form can call the ExitThread() application context method to end the message loop. At this point, Application.Run() will return.
Without knowing more about the heriarchy of your forms and your rules for deciding when to hide forms and when to exit, it is impossible to be more specific.
John Knoeller Jan 07 '10 at 16:27 2010-01-07 16:27
source share