我有 2 种表单:signin
和 control_panel
。登录完成后,我通过 this.Hide()
函数隐藏此表单,同时我创建 control_panel
表单的新对象并通过 newobj 显示它。显示();
。但是当我直接关闭 control_panel
表单时,我看到第一个表单线程仍在运行。我通过 stop_debugging 按钮关闭它。我将如何同时关闭每个线程或整个程序退出。
最佳答案
您的第一个表单的线程仍在运行,因为您只调用 this.Hide
.所做的只是隐藏表单;它不会关闭它。相反,您需要使用 this.Close
,这将关闭您的原始表单。
如果您想确保整个应用程序退出,并在此过程中关闭任何可能仍打开的表单,您可以使用 Application.Exit
表单代码中任意位置的方法。
编辑:要扩展我的上一条评论,您可能需要在 Program.cs
文件中添加类似的内容:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SignInForm frmSignIn = new SignInForm();
if (frmSignIn.ShowDialog() == DialogResult.Yes)
{
//If the sign-in completed successfully, show the main form
//(otherwise, the application will quit because the sign-in failed)
Application.Run(new ControlPanelForm());
}
}
}
关于c# - C# 中线程仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275992/