线程仍在C#中运行

时间:2010-11-25 10:24:27

标签: c# .net winforms forms

我有两种表单:signincontrol_panel。登录完成后,我将this.Hide()函数隐藏此表单,同时我正在创建control_panel表单的新对象并通过newobj.Show();显示它。但是当我直接关闭control_panel表单时,我看到第一个表单线程仍在运行。我正在通过stop_debugging按钮关闭它。如何同时关闭每个线程或整个程序退出。

2 个答案:

答案 0 :(得分:3)

您的第一个表单的主题仍然在运行,因为您只调用了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());
      }        
   }
}

答案 1 :(得分:0)

在control_panel的control_panel表单属性窗口中创建一个FormClosed事件,并将以下行写为

private void control_panel_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }