how to handle application termination

  • Thread starter Thread starter coltrane
  • Start date Start date
C

coltrane

I am writing a simple winforms app and I need to clean up some threads
and network connections prior to termination. Where should I put this
code?

thanks for the help

john
 
That depends on how you're terminating the application and what sort of  
cleanup you actually want to do.

However, typically a reasonable place to do various cleanup would be in  
the main form's OnFormClosing() override (if the cleanup might have to  
cancel the closing), the OnFormClosed() override, or even in the Main()  
method of the application, after the call to Application.Run().

Pete

thanks, that a big help
 
I am writing a simple winforms app and I need to clean up some threads
and network connections prior to termination. Where should I put this
code?

thanks for the help

john

Before that you should first ask how to store the resources (net
connection, etc) in such a way to be accesible to the termination
code.

Regarding the termination code I would hook to AppDomain.ProcessExit
or AppDomain.DomainUnload. The advantage of this is that your code
will run the same even if used in another type of app (console, win.
service, etc)
 
Back
Top