Application.Exit 不是关闭我的程序,还有其他选择吗?
Application.Exit Isn't closing my program, any alternatives?
所以在我的表单构造函数中,我检查了一个参数。如果我得到两个特殊参数之一,那么我只想 register/unregister 我的服务器并关闭程序。我不希望在这些情况下加载表单。然而,由于它目前成功地支持以下代码 registers/unregisters 服务器,但它并没有像我想要的那样立即终止我的应用程序。还有其他命令可以这样做吗?或者也许有更好的方法来完成我想做的事情?
我的代码:
public Form1()
{
InitializeComponent();
instance = this;
fillMeasView();
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
switch (args[1])
{
case "register":
try
{
slikServer1.RegisterServer();
MessageBox.Show("Server registered successfully!");
}
catch(Exception ex)
{
MessageBox.Show("Error: Could not register server." + "\nAdditional Information: " + ex.Message, "Registering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Exit();
break;
case "unregister":
try
{
slikServer1.UnregisterServer();
MessageBox.Show("Server unregistered successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not unregister server." + "\nAdditional Information: " + ex.Message, "Unregistering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Exit();
break;
default:
MessageBox.Show("This is the cmd line arg:" + args[1]);
break;
}
}
}
你想要:
Environment.Exit(0);
您可以使用 Environment.Exit(0)
。非零退出代码表示错误。
Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.
查看文档后,与 Application.Exit()
的区别就很明显了。
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
但要小心 Environment.Exit()
,因为它会立即终止进程。您可能想调查为什么 Application.Exit()
无法关闭您的应用程序。
您真的不想在表单构造函数中使用此代码。相反,找到你的入口点(通常是 Program.cs
文件中的静态 Main
方法),然后把它放在那里。
Application.Exit
已经有点不好了 - 它表明您并不真正知道此时要退出的是什么。它在您的情况下不起作用的原因是还没有要退出的应用程序 - 启动 winforms 应用程序的通常方式如下所示:
Application.Run(new Form1());
因为你在表单构造函数中,Application.Run
还没有 运行,所以没有消息循环退出(Application.Exit
就是这样做的)。
您还可以从入口点直接访问命令行参数 - 它是 Main
方法的参数。在那里做你所有的决定,只有当你真正想要 运行 GUI 应用程序时,才做 Application.Run
。否则,只需 Main
中的 return,您的应用程序就会结束(假设您当时没有启动任何仍处于活动状态的前台线程)。
所以在我的表单构造函数中,我检查了一个参数。如果我得到两个特殊参数之一,那么我只想 register/unregister 我的服务器并关闭程序。我不希望在这些情况下加载表单。然而,由于它目前成功地支持以下代码 registers/unregisters 服务器,但它并没有像我想要的那样立即终止我的应用程序。还有其他命令可以这样做吗?或者也许有更好的方法来完成我想做的事情?
我的代码:
public Form1()
{
InitializeComponent();
instance = this;
fillMeasView();
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
switch (args[1])
{
case "register":
try
{
slikServer1.RegisterServer();
MessageBox.Show("Server registered successfully!");
}
catch(Exception ex)
{
MessageBox.Show("Error: Could not register server." + "\nAdditional Information: " + ex.Message, "Registering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Exit();
break;
case "unregister":
try
{
slikServer1.UnregisterServer();
MessageBox.Show("Server unregistered successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not unregister server." + "\nAdditional Information: " + ex.Message, "Unregistering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Exit();
break;
default:
MessageBox.Show("This is the cmd line arg:" + args[1]);
break;
}
}
}
你想要:
Environment.Exit(0);
您可以使用 Environment.Exit(0)
。非零退出代码表示错误。
Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.
查看文档后,与 Application.Exit()
的区别就很明显了。
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
但要小心 Environment.Exit()
,因为它会立即终止进程。您可能想调查为什么 Application.Exit()
无法关闭您的应用程序。
您真的不想在表单构造函数中使用此代码。相反,找到你的入口点(通常是 Program.cs
文件中的静态 Main
方法),然后把它放在那里。
Application.Exit
已经有点不好了 - 它表明您并不真正知道此时要退出的是什么。它在您的情况下不起作用的原因是还没有要退出的应用程序 - 启动 winforms 应用程序的通常方式如下所示:
Application.Run(new Form1());
因为你在表单构造函数中,Application.Run
还没有 运行,所以没有消息循环退出(Application.Exit
就是这样做的)。
您还可以从入口点直接访问命令行参数 - 它是 Main
方法的参数。在那里做你所有的决定,只有当你真正想要 运行 GUI 应用程序时,才做 Application.Run
。否则,只需 Main
中的 return,您的应用程序就会结束(假设您当时没有启动任何仍处于活动状态的前台线程)。