user6688667
user6688667

Reputation:

Process continue after closing application c#

I'm beginner in C# and I was trying to make WFA application. But there is one problem. When I run it, it shows first(login) form and if I close it without logging in, it will works fine, but when i login, first form hides, secound form shows up and if i close it by clicking X button, form disappeared but I can see process running in task manager. On secound form is also logout button, and if i click button it hides secound form, and shows first(login) form, and if i close it, still process running in background. How can i stop process running in background when closing App by X button? I was searching but i couldn't find solution for my problem. (i found something to attach to process and see threads but there is too many of them)

There is code where Forms are created hided etc:

If logged in frmLogin hides -> show frmMail
If logged out frmMail hides -> show frmLogin.

If user successfully logged in:

this.Hide();
frmMail f2 = new frmMail();
f2.Show();

If user click "Logout" button:

this.Hide();
frmLogin f1 = new frmLogin();
f1.Show();

I found solution, if everyone have same problem this may help. How to switch between forms without creating new instance of forms?

How do I prevent the app from terminating when I close the startup form?

Upvotes: 2

Views: 6630

Answers (1)

Razvan Ghena
Razvan Ghena

Reputation: 179

What I will do in your case is pretty simple. Fist I will override the FormClose_Event, something like this

void Form_FormClosing(object sender, FormClosingEventArgs e)
{
  //And here you can call 
  Application.Exit(); 
  // which this will cause to close everything in your application
  //Also if you want to be more aggressive, there is another option, you can  
  //use, Environment.Exit(1), which will basically kill you process.
}

I hope my answer helps you, have a nice day!

Upvotes: 2

Related Questions