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

        }
Community
  • 1
  • 1
Relativity
  • 6,690
  • 22
  • 78
  • 128

2 Answers2

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.

Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • 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. – Ed Bayiates Jul 06 '11 at 20:02
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.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73