Abort Startup, when Error occurs in Me.Startup

I am using the ApplicationEvents to prepare global data for my Windows Forms application.

If I have a critical error during this startup, I want to shut the application down.

I can use the e.Cancel = True, to stop the main form for opening.

But I can find what command I can call on the My.Application object, that will force it to start to shut down.

I know it is going to be something silly simple, but I am just drawing a blank.

Thanks



Answer this question

Abort Startup, when Error occurs in Me.Startup

  • John Mark

    e.Cancel is the preferred method for preventing the application from starting. Why doesnt this work for you

  • muk2cl

    Huy Nguyen from the VB team helped to find a solution in another thread. The solution is simple... and makes sense. It helps to keep in mind that SplashScreen is shown on a different thread.

    To abort in MyApplication_Startup do this:

    If Not Me.SplashScreen Is Nothing Then
    While Not Me.SplashScreen.IsHandleCreated
    System.Threading.Thread.Sleep(100)
    End While
    Me.HideSplashScreen()
    End If
    e.Cancel = True



  • genio

    I did find another way to do it, but you have to turn off one of the ThreadChecking monitors.... but since you are exiting the program....

     

            Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
                Try
                    ' Startup Code
                Catch ex As Exception
                    e.Cancel = True
                End Try

                If e.Cancel = True Then
                    Control.CheckForIllegalCrossThreadCalls = False
                    My.Application.SplashScreen.Close()
                End If
            End Sub


  • adamscabana

    After I set e.Cancel = True; the Startup SplashScreen stays up.

    I take it that I must specifically close the Startup SplashScreen then....

    (Yesterday was a long day, and I have been missing the obvious stuff)


  • perryf

    I'm getting this same problem. Closing the startup form doesn't work. The application still hangs. e.Cancel only seems to work when you don't have a SplashScreen set at all. Is this a bug

  • Armadillo

    I think the IsHandledCreated + HideSplashScreen() is way more elegant. ;-)

  • Abort Startup, when Error occurs in Me.Startup