Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()

后端 未结 4 1357
闹比i
闹比i 2020-12-15 02:38

My GUI desktop-based WPF 4.0 (C# .Net 4.0) program works with SQL Server database. Each time when I run my application it creates a connection to SQL Server via ADO.NET Enti

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 03:24

    @Damian Leszczyński - Vash's answer pretty much covers the 4 specific questions you asked. For your final question on Application.Exit(), that's an event you can subscribe to, not a method that you can call. It should be used like this:

    Application.Current.Exit += CurrentOnExit; 
    //this.Exit += CurrentOnExit; would also work if you're in your main application class
    
    ...
    
    private void CurrentOnExit(object sender, ExitEventArgs exitEventArgs)
    {
        //do some final stuff here before the app shuts down
    }
    

提交回复
热议问题