error when closing main form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is the situation.

In my Main Form cosing event, I create a new form (also note, that I call
ShowDialog() for it) which starts a backgroundworker to backup the local
database. After that is done, I let the application continue closing down.
All is dandy fine, as long as the application is closed from the X on the
top right corner or from my main menu Exit button.
The last one simply calls this.Close();

Problems arrise if I would call Application.Exit() or if I have an error in
my app, which causes the CLR error message to show (to you want to close, or
quit the app) and I say "Quit" in that message.

In such a case, I get a CLR error message after the main form closing event.
The error message says something about "enumeration may not fire, because
list has changed".
If I was to say "Quit" to that CLR error message as well, then the form
closing event would be fired again, afterwards I would get the windows error
message (do you want to report to Microsoft or not).

My testing has shown that the CLR doesn't like it, when you create and show
a new form in the form closing event. So this is strictly a CLR issue not
mine...

As of such, what I would like to do, is only show my form when the user
presses the Exit from the main menu or the top right corner X.

Can somebody help me accomplish that? Because currently I haven't found any
working solutions.

I guess I could trap the Form Closing message in the WndProc, but I'm
guessing that message would still be called if I press "Quit" in the CLR
error message.


There is one more strange that I'd like to note:
If I put Application.Exit() in my main menu Exit button, then the main
form's closing event is still fired, although the msdn says that "form closed
and closing events will not fire when Application.Exit() is called".
Is there like an exception to that rule for the main form or something??

Thanks in advance,
 
I figured out how to solve my problem.
I used the formclosing event's object e, and it's property CloseReason.
 
Back
Top