C# 申请.退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application.Exit
提问by George2
I am using VSTS 2008 + .Net 3.5 + C# to develop Windows Forms application. My confusion is, seems Application.Exit does not force application to terminate? If not, which method should I call to make application terminate?
我正在使用 VSTS 2008 + .Net 3.5 + C# 来开发 Windows 窗体应用程序。我的困惑是,似乎 Application.Exit 不会强制应用程序终止?如果没有,我应该调用哪种方法来使应用程序终止?
EDIT 1:
编辑 1:
Normally the main method is like this, how to exit Main function gracefully without calling Environment.Exit?
通常main方法是这样的,如何优雅地退出Main函数而不调用Environment.Exit呢?
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Form1());
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
}
thanks in advance, George
提前致谢,乔治
采纳答案by Marc Gravell
Application.Exit
really just asks the message loop very gently.
Application.Exit
真的只是非常温和地询问消息循环。
If you want your app to exit, the best way is to gracefully make it out of Main
, and cleanly close any additional non-background threads.
如果您希望您的应用程序退出,最好的方法是优雅地退出Main
,并干净地关闭任何其他非后台线程。
If you want to be brutal... Environment.Exit
or Environment.FailFast
? notethis is harsh - about the same as killing your own Process
.
如果你想变得残忍......Environment.Exit
或者Environment.FailFast
?请注意,这很苛刻 - 与杀死自己的人大致相同Process
。
回答by Fredrik M?rk
If your application does not exit gracefully when you call Application.Exit
there is (obviously) something that prevents it from doing so. This can be anything from a form setting e.Cancel = true
in the FormClosing
event, to a thread that is not a background thread that is still running. I would advice you to carefully research exactly what it is that keeps your process alive, and close that in a nice manner. That should make your application close nicely as well.
如果您的应用程序在您调用时没有正常退出,那么Application.Exit
(显然)有一些东西会阻止它这样做。这可以从一个形式设置任何e.Cancel = true
的FormClosing
事件,一个线程不是一个后台线程仍在运行。我建议你仔细研究到底是什么让你的过程保持活力,并以一种很好的方式结束它。这也应该使您的应用程序很好地关闭。
Typically, in a winforms application, it should be sufficient to close the main form.
通常,在 winforms 应用程序中,关闭主窗体就足够了。
回答by Peter Gfader
I use
我用
if (System.Windows.Forms.Application.MessageLoop)
{
// Use this since we are a WinForms app
System.Windows.Forms.Application.Exit();
}
else
{
// Use this since we are a console app
System.Environment.Exit(1);
}
from http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
来自http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
回答by Dan W
I had the same problem when I discovered opening a new form/window within the program, and only HIDING that second form (not closing it), would prevent the main form from properly quitting via Application.Exit();
当我发现在程序中打开一个新表单/窗口时,我遇到了同样的问题,只有隐藏第二个表单(而不是关闭它),才会阻止主表单通过 Application.Exit() 正确退出;
There are two solutions in this case. First is to simply use Close() on the main form instead of Application.Exit(). Second solution is to use the following code:
在这种情况下有两种解决方案。首先是在主窗体上简单地使用 Close() 而不是 Application.Exit()。第二种解决方案是使用以下代码:
if (secondForm != null && !secondForm.IsDisposed) secondForm.Dispose();
回答by James Waldemar Scheidt
Try the following:
请尝试以下操作:
Process.GetCurrentProcess().Kill();
Environment.Exit
doesn't work with Winforms and Environment.FailFast
throws its own exception.
Environment.Exit
不适用于 Winforms 并Environment.FailFast
抛出自己的异常。
回答by Leon
I found that simply all you need to do is insted of doing application.exit and all that you just have to do is put in End Simply enough the End command closes it
我发现您只需要做的就是执行 application.exit 并且您只需要做的就是放入 End 简单地 End 命令关闭它