我如何在启动时出现错误时显示一个消息后,立即退出Windows窗体应用程序? [英] How do I exit a Windows Forms application immediately after displaying a MessageBox when an error on startup occurs?

查看:116
本文介绍了我如何在启动时出现错误时显示一个消息后,立即退出Windows窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在Windows启动时窗体应用程序我看了一个XML文件。如果这个文件不存在或不具备一定的设置,我想显示一个消息框向用户说明问题,然后关闭应用程序。

At startup of a Windows Forms application I read a XML file. If this file doesn't exist or doesn't have a certain setting I want to display a MessageBox to the user indicating the problem and then close the application.

我想这样做的加载 Application.Exit()像这样的事件的主要形式

I tried to do that in the Load event of the main form with Application.Exit() like so:

private void MainForm_Load(object sender, EventArgs e)
{
    if (!StartupSettingsCorrect())
    {
        MessageBox.Show("Blabla... Can't start application.");
        Application.Exit();
    }

    // Other stuff...
}

不过,这似乎并没有要干净。 (该应用程序正在运行,但隐形在屏幕上没有任何形式的。)

But this doesn't seem to be clean. (The application is running but "invisible" with no form on the screen.)

什么是不是干净关闭在这种情况下,最好的方式和地点?

What is instead the best way and place for a clean shutdown in this situation?

感谢您的帮助提前!

推荐答案

Environment.Exit()应该做的伎俩。至少它在你所描述的情况为我工作。

Environment.Exit() should do the trick. At least it worked for me in the situation you described.

根据 MSDN ,这种方法

终止这个过程,并给出了底层操作系统指定的退出code。

Terminates this process and gives the underlying operating system the specified exit code.

这篇关于我如何在启动时出现错误时显示一个消息后,立即退出Windows窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆