How to prevent WPF application from loading?

I want the WPF application to run only under certain conditions. I tried the following without success:

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

But the application works fine even when the condition is not met

+5
source share
3 answers

Try the following:

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

Another solution

Constructor

<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 for

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

, , , / , , - . , - , IMMEDIATELY , , MVVM Locator , .

, , , .

, :

1) MVVM Locator App.xaml.

2) Application_Startup

:

    #region Handlers For Unhandled Exceptions
        // anything else to do on startup can go here and will fire after the base startup event of the application
        // First make sure anything after this is handled
        // Creates an instance of the class holding delegate methods that will handle unhandled exceptions.
        CustomExceptionHandler eh = new CustomExceptionHandler();

        AppDomain.CurrentDomain.UnhandledException +=
             new UnhandledExceptionEventHandler(eh.OnAppDomainException);
        // this ensures that any unhandled exceptions bubble up to a messagebox at least
        Dispatcher.CurrentDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(eh.OnDispatcherUnhandledException);

        #endregion  Handlers For Unhandled Exceptions

3) Application_Startup App.xaml .

 Startup="Application_Startup"   <<<<  this name is arbitrary but conventional AFAICT

4) Applicaton_Startup ViewModelLocator :

            Resources.Add("Locator", new ViewModelLocator());
             //You can use FindResource and an exception will be thrown straightaway as I recall
            if (!(TryFindResource("Locator") == null))  

                throw new ResourceReferenceKeyNotFoundException("ViewModelLocator could not be created", "Locator");

5) Then, immediately after discovering the resource, open MainWindow, but only if the locator was successfully created

            Uri uri = new Uri("pack:Views/MainWindow.xaml", UriKind.RelativeOrAbsolute);
            Application.Current.StartupUri = uri;

Step (4) will immediately throw an exception if the constructor on the locator does not work, WHICH ASKS ALL THE TIME ME RESUMES.

Then, the exception from step 4 is handled as follows (RadMessageBox is used in this example, but feel free to fix it:

public void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        try
        {

                var result = this.ShowExceptionDialog(e.Exception);

        }
        catch
        {


            RadMessageBox.Show("Fatal Dispatcher Error - the application will now halt.", Properties.Resources.CaptionSysErrMsgDlg,
               MessageBoxButton.OK, MessageBoxImage.Stop, true);
        }

        finally
        {

            e.Handled = true;

            // TERMINATE WITH AN ERROR CODE -1!
            //Environment.Exit(-1);
        }
    }
0
source

All Articles