2

When i need to exit a WPF Application i usually use Application.Current.Shutdown() but sometimes the Application.Currentis NULL,I also tried Environment.Exit(0) but it doesn't work correctly. Is there any other way to exit a WPF Applicationspecialy when Application.Current is NULL?

this is my code:

 public partial class App : Application
 {
    protected override void OnStartup(StartupEventArgs e)
    {
      if (something)
        {
          continue....
        }
      else
        {
        Application.Current.Shutdown()
        }
  }
mahboub_mo
  • 2,908
  • 6
  • 32
  • 72
  • From what context you're calling Application.Current? could you post code you're using? – Shahin Oct 23 '14 at 08:54
  • I want to close my entire application!and there is just one `System.Windows.Application` for any `AppDomain`. – mahboub_mo Oct 23 '14 at 08:56
  • 1
    Have you tried this.Close(); ?? – apomene Oct 23 '14 at 08:57
  • @apomene:what do you mean by this? I use this code in `App.xaml` and the in no This here!!! – mahboub_mo Oct 23 '14 at 08:58
  • 3
    the first time I've heard that `Application.Current` could be null. At least right after the main window was loaded, there is no reason `Application.Current` could be null. – King King Oct 23 '14 at 09:03
  • @KingKing:When you close a window(the only window that is open)and do something in the middle time before you open the next window ,the application would be shutdown so Application.Current would be null although it's still running. – mahboub_mo Oct 25 '14 at 16:18

2 Answers2

5
System.Diagnostics.Process.GetCurrentProcess().Kill();

Anywhere and anytime this terminates your application immediately.

Demir
  • 1,787
  • 1
  • 29
  • 42
0

This is obviously per design. why would there be an application available when its startup didnt complete yet. Just do this.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (false)
        {
            //
        }
        else
        {
            this.Shutdown();
        }
    }
}
Dbl
  • 5,634
  • 3
  • 41
  • 66