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

2

5

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();
            }

        }
Venatic answered 6/7, 2011 at 19:31 Comment(5)
You need to show the code surrounding Application.Current.Shutdown(). The problem may have to do with where you're calling it from.Rapids
My Code is in App.Xaml -> OnStartup(StartupEventArgs e)Venatic
Can you post your entire App.xaml.cs file contents?Jenson
Which specific code you are looking for ?...Because copying entire code will messup things.Venatic
Please put the entire OnStartup method for now. Just put 4 spaces in front of each line to make it format properly. If that doesn't show enough, the code in it will probably tell us what else we need to see.Rapids
G
14

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

Ghazi answered 6/7, 2011 at 19:53 Comment(1)
Well yes -- you would have to explicitly call Dispose() on your objects first, since Environment.Exit will terminate your threads. Application.Current.Shutdown won't call Dispose either; nothing will. There is no auto-dispose in .net.Ghazi
D
0

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.

Daniel answered 6/7, 2011 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.