I was having a terrible problem getting a Splash screen to show until it was
done and then showing the Main form. Using some ideas from this thread
[
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/casoast.asp]
I was able to solve my problems. I will do my best to copy/paste my code in
hopes that it will help.
The following code contains 3 classes (2 of which are Forms [1 Main/1 Splash])
I will omit the Designer code and any unnecessary code.
'Apploaded.vb - Entry point for the application
Public Class AppLoader
Private Shared mForm As MainForm = New MainForm
<STAThread()> _
Shared Sub Main(ByVal args() As String)
Application.Run(mForm)
End Sub
End Class
'MainForm.vb
Imports System.Threading
Public Class MainForm
Inherits System.Windows.Forms.Form
'<Omitted code here>
Shared splash As SplashScreen = New SplashScreen
Const iMinAmountOfSplashTime As Integer = 5
Public Sub StartSplash()
' Run the form
Application.Run(splash)
End Sub
Private Sub CloseSplash()
If (splash Is Nothing) Then Return
' Shut down the splash screen
splash.Invoke(New EventHandler(AddressOf splash.KillMe))
splash.Dispose()
splash = Nothing
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim bShowSplash As Boolean = True
bShowSplash = RegistryAccess.GetStringRegistryValue("Show Splash",
TypeCode.Boolean, True)
If (bShowSplash) Then
' Create a new thread from which to start the splash screen form
Dim otter As New ThreadStart(AddressOf Me.StartSplash)
Dim splashThread As Thread = New Thread(otter)
splashThread.ApartmentState = ApartmentState.STA
splashThread.Start()
' Pretend that our application is doing a bunch of loading and
' initialization
Thread.Sleep(iMinAmountOfSplashTime / 2)
splash.Show()
' Loop until the Splash Screen is done
While (splash.IsRunning())
'My application has an animation
'Application.DoEvents() is used to
'to make sure the Animation is shown
Application.DoEvents()
End While
End If
' Close the splash screen
CloseSplash()
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If (Not splash Is Nothing) Then Return
MyBase.OnPaint(e)
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
If (Not splash Is Nothing) Then Return
MyBase.OnPaintBackground(pevent)
End Sub
Protected Overrides Sub OnClosing(ByVal e As
System.ComponentModel.CancelEventArgs)
' Make sure the splash screen is closed
CloseSplash()
MyBase.OnClosing(e)
End Sub
End Class
'SplashScreen.vb
'The m_bDone is set to True when the following conditions are met:
' a. The Splash Screen has finished loading
' b. The user clicks on the link to begin (not shown)
Public Class SplashScreen
Inherits System.Windows.Forms.Form
'<Omitted code here>
Private m_bDone As Boolean = False
'Called by MainForm::Load (Event)
Public Function IsRunning() As Boolean
Return Not m_bDone
End Function
'Called by MainForm::CloseSplash
Public Sub KillMe(ByVal o As Object, ByVal e As EventArgs)
'Dispose any graphics or other objects here
Me.Close()
End Sub
End Class
Thats pretty much it. The project uses the AppLoaded class as the start up
object (via Main). The original thread used a timer and mose of the code can
easily be ported to VB.NET. I hope this helps. Any question please reply or
email me at (e-mail address removed) with "Splash Screen" as the subject.
Thanks and best wishes!!