2010-11-25 63 views
2

我有兩種形式:signincontrol_panel。完成登錄後,我通過this.Hide()函數隱藏此表單,同時我正在創建control_panel表單的新對象,並通過newobj.Show();顯示它。但是當我直接關閉control_panel窗體時,我看到第一窗體線程仍在運行。我正在通過stop_debugging按鈕關閉它。我將如何同時關閉每個線程或整個程序退出。線程仍在C#中運行

回答

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()); 
     }   
    } 
} 
+0

當我通過thhis.close()方法關閉窗體時,我的第二個窗體正在顯示。我不明白爲什麼? – 2010-11-25 10:38:24

+1

我假設你是指當你關閉第一個表單時,你的應用程序退出了?這是因爲在`Program.cs`文件中,你有'Application.Run`設置爲你的第一種形式。每當第一個窗體關閉時,消息循環結束並退出應用程序。我強烈建議您將啓動邏輯(即顯示和處理登錄表單)放在Program.cs文件中,而不是將登錄表單設置爲啓動表單,因爲您計劃在沒有應用程序退出。 – 2010-11-25 10:43:42

0

創建CONTROL_PANEL的CONTROL_PANEL表單屬性窗口FormClosed事件,並寫入以下line as

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