tags:

views:

34

answers:

2

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

+2  A: 

try this.....

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

Another solution

Designer

<Application x:Class="SingleInstanceWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">
</Application>

Code behind

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (ConditionIsMet)
        {
            var window = new MainWindow();
            window.Show();
        }
        else
        {
            this.Shutdown();
        }
    }
}
Jader Dias