1

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

cmrhema
  • 981
  • 2
  • 16
  • 28

3 Answers3

1

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
  • 339,232
  • 124
  • 596
  • 636
1

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

Does Application.ApplicationExit event work to be notified of exit in non-Winforms apps?

Community
  • 1
  • 1
Andy White
  • 86,444
  • 48
  • 176
  • 211
0

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
  • 184
  • 1
  • 2
  • 15