【发布时间】:2016-11-23 02:18:43
【问题描述】:
假设在WindowsFormsApplicationBase.OnCreateMainForm() 的时候出了点问题,我该如何“温和地”退出应用程序?我想退出就像使用按下关闭按钮一样,所以我猜Environment.Exit() 不太适合,因为它会立即终止应用程序并且可能不允许应用程序自行清理。
我的代码如下所示:
public class MyApp : WindowsFormsApplicationBase
{
public MyApp()
{
this.IsSingleInstance = true;
}
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new splashForm();
}
protected override void OnCreateMainForm()
{
if(!do_something()) {
/* something got wrong, how do I exit application here? */
}
this.MainForm = new Form1(arg);
}
还有我的Main() 函数:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new MyApp().Run(args);
}
【问题讨论】:
-
应用程序尚未启动。它仍然只是初始化,Application.Run() 调用稍后发生。所以 Application.Exit() 不能工作。当然,Environment.Exit() 会轻松完成工作。
-
Environment.Exit() 将退出而不是杀死应用程序?如果是这样,我想这是要走的路
标签: c# .net winforms error-handling splash-screen