views:

75

answers:

2

I have a project that requires that I run some code when the application closes. The application is only a single form that hides itself almost immediately after execution, so the only way it can be closed is by manually terminating it with the task manager.

If I use the onclose event, will it be called in this case? Also, can someone post a brief code example for setting up the onclose event?

+5  A: 

In your Program.cs before showing the form add this:

Application.Exit += ....
BFree
Very nice, thanks!
chris12892
+1  A: 

Yea - I got dinged when my windows(less) app didn't shut down with a system shutdown request (you know "terminate this process" type dialog message). You can subscribe to specific requests, for example:

SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_Shutdown);

and the shutdown handler does a clean up (your app) and shutdown:

Application.Exit();

Maybe BFee's has a better answer.

ddm
I actually like this a little better. Do you mean ApplicationExit, BTW? There is no Application.Exit, but there is Application.ApplicationExit
chris12892
Microsoft does list an Application.Exit() in short "Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed."
ddm