Search code examples
c#.netwpfstartup

How to prevent a WPF app from loading?


I want that the WPF application starts only in certain conditions. I tried the following with no success:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (ConditionIsMet) // pseudo-code
        {
            base.OnStartup(e);
        }
    }
}

But the app runs normally even when the condition is not met


Solution

  • Try this:

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        if (MyCondition)
        {
            ShowSomeDialog("Hey, I Can't start because...");
            this.Shutdown();
        }
    }