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