Threads are still running in C#
I have 2 forms: signin
and control_panel
. After signin done I'm hiding this form by this.Hide()
function and same time I am making new object of control_panel
form and showing it by newobj.Show();
. But when I am closing directly control_pane开发者_如何学Cl
form, I am seeing first form thread are still running. I am closing it by stop_debugging button. How will I close every threads or whole program exit simultaneously.
The thread for your first form is still running because you're only calling this.Hide
. All that does is hide the form; it doesn't close it. Instead, you need to use this.Close
, which will close your original form.
If you want to make sure that your entire application exits, and in the process close any forms that may still be open, you can use the Application.Exit
method anywhere in your form's code.
EDIT: To expand on my last comment, you might want something like this in your Program.cs
file:
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());
}
}
}
Create a FormClosed event in control_panel form property window of control_panel and write the following line as
private void control_panel_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
精彩评论