tags:

views:

43

answers:

3

Hi,

I have a windows forms application, where I have declared some static variables. On the click of exit button, I have disposed of some datatable which i have declared static.

Many a times the user instead of clicking the exit button, will just exit the windows application by clicking the X button on the left corner top.

What should be done to ensure that even if the user clicks the X button, everything is disposed of properly.

Thanks

Regards

Hema

+1  A: 

Just add a delegate function to the Closing event of the form.

this.Closing += this.MyForm_Closing;

You can also use the Closed event of the form if you'd prefer it gets called after the form is closed.

Brian R. Bondy
+1  A: 

This question has some good descriptions of events that you can hook into to detect when a application is exiting.

http://stackoverflow.com/questions/529867/does-application-applicationexit-event-work-to-be-notified-of-exit-in-non-winform

Andy White
A: 

You can add an event handler to dispose your variables when the form is closing.

private: System::Void myDialog_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
         // Dispose your static variables here
     }
manuel