请告诉我这些方法用于关闭我的应用程序之间的区别?我的、告诉我、应用程序、区别

由网友(孤绝如初见)分享简介:基本上我有一种主要形式,其在加载,对登录用户打开一个子窗体。当他们取消或关闭此登录表单,我需要关闭整个应用程序。Basically I have a main form which upon loading, opens a child form for logging in the user. When they...

基本上我有一种主要形式,其在加载,对登录用户打开一个子窗体。当他们取消或关闭此登录表单,我需要关闭整个应用程序。

Basically I have a main form which upon loading, opens a child form for logging in the user. When they cancel or close this login form, I need to close the whole application.

但似乎有一些不同的方法来关闭一个C#程序:

But there seems to be a few different ways to close a C# program:

Application.Exit();

Application.ExitThread();

Environment.Exit(1);

Process.GetCurrentProcess()杀死();

SFTPClient.LDAPLoggedIn = FALSE;关闭();

编辑:很抱歉,如果这个人是不明确的:它设置一个控制器对象的属性,以指示登录失败。打开子窗体后,我会检查父窗体这个属性,看程序是否应该继续。它基本上转移退出程序到父无例外的责任。的

6:抛出新的异常(用户关闭表);

我可以看到,有处理这两种方式:

I can see that there are two ways of handling it:

告知出现了错误的父(如5和6。) 从子窗体关闭程序。

时的这两种认为是更好的做法?

Is either of these two considered better practice?

每个方法似乎对我的程序相同的效果,但他们如何真正比较?

Each approach seems to have the same effect on my program but how do they actually compare?

更新: 感谢您的答案。对于那些在未来和好奇的人搜索这个问题,这是我的最终解决方案:

UPDATE: Thanks for the answers. For those searching this question in the future and curious people, this was my solution in the end:

private void FormMain_Load(object sender, EventArgs e)
{
    if (new FormLogin().ShowDialog(this) == DialogResult.Cancel) Close();
}

private void buttonCancel_Click(object sender, EventArgs e)
{
    Close();
}

我发现,当一个方式是通过点击X关闭,DialogResult的设置为自动取消所以我需要做的就是关闭()

推荐答案

如果你是想在最后一种情况下优雅地处理异常,这是的确定的(不是很大,虽然) - 只要是它是一个特殊的情况,您的应用程序。否则,我会创建一个新的方法,它显示了形式用布尔的对话框。如果布尔回来假(即,用户关闭的形式),然后,我会处理应用程序关闭从那里(使用 Application.Exit())。

If you are wanting to gracefully handle the exception in the last case, that's ok (not great though) - as long is it is an exceptional situation to your application. Otherwise I'd create a new method that shows the form as a dialog with a boolean. If the boolean comes back false (aka, user closed the form) I would then handle the application shut down from there (Using Application.Exit()).

这是在我的愚见,非常不好的做法,关闭一个孩子的应用程序,而不是告诉父母。唯一的一次,我同意这是一个 FAILFAST 的情况,这是非常罕见的。

It is, in my humble opinion, very bad practice to close the application from a child rather than telling the parent. The only time I agree with this is in a FailFast situation, which are very rare.

应用程序。退出();

此方法将停止所有线程所有正在运行的消息循环,并关闭应用程序的所有窗口。此方法不强制应用程序退出。 Exit方法通常称为从消息循环内,并强制执行返回。要退出一个消息循环当前线程只,调用ExitThread。

This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

应用程序。了ExitThread();

见上面。

Environment.Exit(1) ;

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

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

Process.GetCurrentProcess( ).Kill();

杀死强制终止的过程中,而CloseMainWindow只请求终止。当用图形界面的处理正在执行,其消息循环是处于等待状态。消息循环执行视窗消息由操作系统发送到过程每一次。

Kill forces a termination of the process, while CloseMainWindow only requests a termination. When a process with a graphical interface is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system.

SFTPClient.LDAPLoggedIn = FALSE;关闭();

在在注释澄清(通过流回到母体,并从那里处理),这是迄今为止这样做的最佳方式。

After the clarification in the comment (passes flow back to the parent and handles from there), this is by far the best way of doing this.

抛出新的异常(用户关闭表);

抛出异常调用进程。如果这是主线程,它会在一个非常丑陋的方式抛出异常。

Throws an exception to the calling process. If this is the main thread, it will throw the exception in a very ugly way.

阅读全文

相关推荐

最新文章