Kat
Kat

Reputation: 2500

Is there a way to extend splash screen into form shown event?

I recently was dealing with this error: BeginInvokeStackflowError

I am using threading,and according to my research it is because within the threading .start() event it calls .invoke. If that is done in the mainform_Load event, before it is ready, then you get a BeginInvoke error.

So I've move my code from the load to the shown event. However, there is a lot of stuff going on in the background that I don't want the user to see. Is there a way in my code to extend the splashscreen I have to wait until the mainwindow shown is finished for only the first time?

 Private Sub MainWindow_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    'update table /search network       
    updateTable()
    'clean 
    cleanupTable()
    'fix label 
    updateLabel()
End Sub

Upvotes: 0

Views: 1613

Answers (2)

Your app can be started other than the default "MainForm" method provided by the VB Application Framework. This will use a Sub Main as the starting point allowing you to control what forms show and when, and what happens before that:

' IF your form is declared here, it will be
' available to everything. e.g.:
' Friend mainfrm As Form1

Public Sub Main()
    ' this must be done before any forms/controls are referenced
    Application.EnableVisualStyles()

    ' the main form for the app
    ' just "mainfrm = New Form1" if declared above
    Dim mainfrm As New Form1

    ' eye candy for the user
    Dim splash As New SplashScreen1
    splash.Show()

    ' your code here to do stuff.
    ' you can also invoke your procedures on the main form
    ' mainfrm.FirstTimeSetup()

    ' for demo purposes
    System.Threading.Thread.Sleep(2500)

    ' close/hide the splash once you are done
    splash.Close()
    ' see note

    ' start the app with the main form
    Application.Run(mainfrm)

End Sub
  • Add a module to the project, typically "program"
  • Add your Sub Main
  • Go to Project Properties, uncheck use Application Framework
  • Select Sub Main in the the StartUp object drop down

If you declare the splash screen as Friend at the top, you can truly extend it until all the form's load event is complete and close it there/then.

Upvotes: 1

Kieran Crown
Kieran Crown

Reputation: 93

There is no way to extend the splash screen if you've implemented it using the project settings, however you can use the splash screen form as the initial form instead of your main form. As for waiting until the thread has finished to show the form (or hide the splash screen) consider using a public Boolean in the main form and change it to True once the thread has completed. You can use a timer on the splash screen to check for this Boolean change and then change the form's opacity back to 1.

Upvotes: 0

Related Questions