C#-手动关闭表单应用程序
我想知道完全终止表格申请的适当方式。我使用以下代码以标准方式打开表单:C#-手动关闭表单应用程序,c#,winforms,C#,Winforms,我想知道完全终止表格申请的适当方式。我使用以下代码以标准方式打开表单: namespace History { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(fa
namespace History
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DisplayHistory());
}
}
}
…这里是DisplayHistory函数的一个基本版本,其中包含我试图终止应用程序的各种内容。前两个导致未处理的异常,声明我无法访问已处置的对象。第三个根本没有效果,第四个根本不会编译,因为我收到一个错误,说“应用程序不包含当前的定义”:
与其尝试在构造函数中关闭它,不如不要打开表单 但是,如果您确实希望基于某些条件在构造函数中关闭表单,则可以在代码中的
InitializeComponent
之前订阅Load
事件,并使用this.close()关闭表单
:
要正常关闭应用程序,您可以在表单构造函数之外的任何位置调用以下代码:
this.Close()代码>
Application.Exit()代码>
要强制应用程序停止或异常关闭,可以调用任何位置,包括表单构造函数:
Environment.Exit(1)代码>
请显示所有相关代码。您显示的代码,包括this.Close()
,应该可以正常工作。我在代码中没有看到Application.Exit()或此Close?谢谢。出口(1)正是我想要的。
public DisplayHistory()
{
InitializeComponent();
// this.Close();
// Close();
// Application.Exit();
// Application.Current.Shutdown();
}
public Form1()
{
if (some criteria)
{
this.Load += (sender, e) => { this.Close(); };
}
return;
InitializeComponent();
}