0

I have exit command:

void CloseMethod()
{
   //Application.Current.Shutdown();
     Environment.Exit(0);
}

The problem is that when I click the button it hides the GUI but I still see the app open at the task manager same with Application.Current.Shutdown();

Cid
  • 14,968
  • 4
  • 30
  • 45
Codey
  • 487
  • 1
  • 8
  • 27
  • So on button click you want to close the application? Is it Winform? – Rahul Mar 26 '19 at 11:40
  • @Rahul it's in wpf – Codey Mar 26 '19 at 11:44
  • @Jan but why the app still open at tak manager??? – Codey Mar 26 '19 at 11:47
  • sorry, didn't know it was WPF. for WPF the answer should be this one: https://stackoverflow.com/questions/2820357/how-do-i-exit-a-wpf-application-programmatically – Jan Mar 26 '19 at 11:49
  • Are you sure the process shown in `Task Manager` is not an old instance, before you made your most recent changes? – mjwills Mar 26 '19 at 11:58
  • There was a bug in a Windows DLL (uxtheme.dll) that could trigger a deadlock at program exit. It got fixed in a Windows Update, came back later again and had to be fixed yet again. So you want to first make sure that this machine is actively being maintained by WU. Next thing to look at is the installed anti-malware product, the crapware does not deal well with executable files appearing from seemingly nowhere on a programmer's machine. Temporarily disable it to see if that makes a difference. – Hans Passant Mar 26 '19 at 12:09

2 Answers2

0

You would want to call Application.Exit(); rather which would terminates all message loops and closes all windows, giving your forms the possibility to execute their cleanup code. See Application.Exit for more information. Quoting from documentation

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

For WPF you should call Application.Current.Shutdown(); but read the documentation which clearly says that Shutdown() method should be called from Main() method cause that's the entry point from where Application instance got created

Rahul
  • 76,197
  • 13
  • 71
  • 125
-2

Are you using any threads in the application ? In case you are, make sure you stop all the threads before exiting the application. I don't normally prefer the .abort command, but you can try using

th.Abort(); //th is the name of the thread

before you use Environment.Exit(0)

Hassnain Ali
  • 222
  • 3
  • 12