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 <a href="https://learn.microsoft.com/en-US/dotnet/api/system.windows.forms.application.exit?redirectedfrom=MSDN&view=netframework-4.7.2#overloads">Application.Exit</a> 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