我需要以相同的方式关闭我的应用程序,无论是直接关闭还是从任务栏或任务管理器关闭.每次关闭应用程序之前,我都需要进行一些预设置并自动生成日志文件...
例如,我每次退出时都会对我的申请表格给出以下效果,但是当我从任务栏或任务管理器关闭我的应用程序时,不会显示此效果...
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();
}
Run Code Online (Sandbox Code Playgroud)
从这个问题:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
Application.Run(new MainForm());
}
Run Code Online (Sandbox Code Playgroud)
这比使用FormClosed仅仅因为表单关闭并不一定意味着应用程序(当然特定于您的代码)更可取.