Reputation: 511
I'm facing some problem, when i start my program i run WindowForm1 and then user had to type password and username if it's correct then it's exit form1 and run form2. If user type wrong password or username then he had chance to retype this or he can Close program with the "red X" - which works ok. But when the password and username were correct and he had Form2 running when he press "close" application still run in the background. Can you please give me any info how to close this app when he press close on Window Form 2.
Upvotes: 0
Views: 259
Reputation: 30022
Use Environment.Exit
on the closed event handler:
public Form2()
{
InitializeComponent();
this.FormClosed += Form2_FormClosed;
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
Docs:
Exit terminates an application immediately, even if other threads are running. If the return statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminate
You can also use Application.Exit which:
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Upvotes: 2