473,495 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Showing a form after Application.Exit()... :@!!!

Logan1337
38 New Member
It almost seems as though Microsoft wants it to be illegal to show a form after the user chooses Application.Exit, yet that is exactly what I need to do.

I have a cleanup routine that deletes old articles from a database, and like to show a progress dialog while this is happening, just like Outlook's emptying deleted items dialog.

However, no matter what I try it won't work! Basically my first attempt was to simply show this dialog in the FormClosing event handler of my main form, which "works", but as soon as it closes I get a fatal exception, something about a collection being changed and invalidating an enumerator... some Boolean collection somewhere. *shrug*

Next I tried simply showing the dialog after the Application.Run(mainForm) completes, but as soon as the dialog is created it's immediately destroyed for some reason! I even tried a second Application.Run( cleanupDialog ) but with the same result!!

What else can I do?!
Sep 17 '07 #1
6 1738
Plater
7,872 Recognized Expert Expert
It almost seems as though Microsoft wants it to be illegal to show a form after the user chooses Application.Exit, yet that is exactly what I need to do.
Well generally when I close a program I expect it to close, so yes, I would say showing a form after Application.Exit() should be illegal.

I think what you want is for a user to say "i want to close" and then you show your dialog/forms whatever and THEN call Application.Exit();

You can either make your own exit buttons (and remove the X from the window) or you can override the events into waiting to close down until you say so.
Sep 17 '07 #2
Logan1337
38 New Member
But this does not account for things like system shutdown, which I would also like for the cleanup operation to occur for, as does Outlook.

It seems to me there must be a way to do it without changing the Exit() call, or perhaps cancelling the exit altogether. I'm going to try that...
Sep 17 '07 #3
Plater
7,872 Recognized Expert Expert
You could override the message pump to trap the messages and perform operations.

And outlook and other applications are not written in managed code, so they use native APIs (which you could too)
Sep 17 '07 #4
Logan1337
38 New Member
You could override the message pump to trap the messages and perform operations.
How might I go about doing that?
Sep 17 '07 #5
Plater
7,872 Recognized Expert Expert
google overriding the close function in .NET
Sep 17 '07 #6
Logan1337
38 New Member
Ok I think I've come up with a solution. It involves showing the cleanup dialog in the FormClosing event as before, but this time I'm closing the window before calling Application.Exit(). In fact, I don't even need to call Application.Exit at all, since closing the main form of the application has the same effect.

However I did have to set a little flag that indicates the form should in fact close, because I also happen to have a "close to system tray" option that bypasses the actual close (cancels it) when the close box is pressed. Setting a flag allows me to know that I should actually close the window instead of minimizing it.

Anyhow it seems to work so that's another hurdle down.
Sep 19 '07 #7

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.