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

前端 未结 2 813
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 05:58

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

2条回答
  •  天命终不由人
    2020-12-07 06:41

    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.

提交回复
热议问题