Really closing my application [CS/C# 2005]

This topic is 4367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This is a really annoying and yet probably trival issue but ... I have a main form (frmMainMenu) where the users have a couple of choices of other forms to view, when they do that frmMainMenu is hidden (as it is no longer required but it is the starting point of everthing so can't just get rid of it). Anyways - so frmMainMenu is hidden (this.Hide()) and it creates and .Shows other forms, these in turn may also lead to other forms (where they will then be hidden) and so on... Now to the problem - funny thing is when all is said and done and I EXIT (hit the 'x' in the top-right corner of the window) I expect my application (all of it) to close but instead it remains in memory (I have to kill it manually via the process list)... I assume this is because when I click the 'x' it only closes THAT form and all the hidden ones that came before it are still running but are just "hidden"... Well when I hit the 'x' on ANY of my forms I want the entire application to close (gracefully, no leaks due to forms, etc...) as it should... Any ideas, hints, and help would be greatly appreciated, thanks

Share on other sites
I would have a single entry point for the application that handled creation and termination of all forms. If a form needs to open another form, have a loopback event handler on some button that informs the main app to create another form and close the current one. This way you wouldn't have to keep unused hidden forms open. You can also hook the form's closing event and then if no other forms are open, the main app can close.

Regards,
ViLiO

Share on other sites
Wouldn't that be an acceptable solution? In the event when the 'x' of any form is pressed?

Because I am not sure how to accomplish your solution (with the events)... Sounds rather complicated... but it is something to look into for the future...
I just need a quick fix that won't cause any negative side effects...

Thanks,

Share on other sites
Somewhere, probably in your Program.cs, you should have a line like this:
Application.Run(MainForm);

When the Form passed to Application.Run() is closed, the main thread will return from Application.Run() and the entire application should terminate.

If that isn't the case, you probably have additional threads running that are not cleanly shut down and do not have the .IsBackground property set. Threads without this property keep the application alive while those where it is set will be forcefully terminated if you forget to shut them down cleanly.

-Markus-

Share on other sites
Application.Exit() will set a flag that causes the message pump run by Application.Run() to terminate, effectively causing the main thread to return from Application.Run() just as if you clicked on the X of your main window.

If your call Application.Exit() after Application.Run() has finished, it will have no effect.

-Markus-

Share on other sites
Cygon: Problem isn't that threads are keeping my application open (I think) - thing is the form that runs in Application.Run(MainForm); is the one I hide and therefore the user never exits it (it is hidden), instead the user will close the forms spawned by MainForm (thinking this will close the application).

Regarding Application.Exit - if I put it in the "FormClosing" event of all my forms it seems to be working - but is this OKAY to do? Isn't this just like killing (ending the process via task manager) which could be bad and cause negative side effects?

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632593
• Total Posts
3007277

×