Application.Current.Shutdown(-1) not closing WPF app

筅森魡賤 提交于 2019-11-28 04:31:06

问题


I am checking if the windows authenticated user is a valid user for my wpf application or not.

If not, I need to shut down the application; but even after executing Application.Current.Shutdown(-1) the application keeps on executing happily.

The below link says that I need to remove my StartUpURI; but I dont have that tag in my app.xaml. -> Shutting down a WPF application from App.xaml.cs

EDIT :- I have this code in APP.XAML.CS ->

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            this.exceptionPolicy = ConfigurationManager.AppSettings.Get("ExceptionPolicy");
            this.displayErrorDetails = true;
            this.container = new UnityContainer();

            // Register services and types in Unity
            RegisterServices();

            // Check user
            if (!IsValidUser())
            {
                //Application.Current.Shutdown(); 
                App.Current.Shutdown();
            }

        }

回答1:


Use Environment.Exit() instead. That will try to shut down gracefully, but if it can't gracefully, will shut down rudely -- forcefully terminating threads.




回答2:


I have never had luck shutting something down from the start-up. I would suggest starting a new Thread that, after some brief delay, shuts down the application using similar code that you have in your sample.



来源:https://stackoverflow.com/questions/6601875/application-current-shutdown-1-not-closing-wpf-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!