I need to close my application in the same way, whether it is closed directly or closed from task bar or task manager. I need to do some pre-settings and automatically generate log files each time before the application is closed…
For example I give following effect to my application form each time it exits, but this effect is not shown when I close my application from task bar or task manager…
System.Windows.Forms.Timer closeTimer = new System.Windows.Forms.Timer();
void lblClose_Click(object sender, System.EventArgs e)
{
closeTimer.Tick += new EventHandler(closeTimer_Tick);
closeTimer.Interval = 10;
closeTimer.Start();
}
void closeTimer_Tick(object sender, EventArgs e)
{
int a = (int)(this.Opacity * 100);
a--;
this.Opacity = ((double)a / 100);
if ((this.Opacity*100) == 0)
this.Close();
}
I’m assuming your app is a System.Windows.Forms.Application. If so you can listen to ApplicationExit event.
So in Main, do something like:
See http://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx