This tip was written for Visual Basic.NET 2005 and assumes the Splash Screen is called using the Application's splash screen property.
If your application does a significant amount of initialization work (more than a second or two), you may want to tell the user that the app is making progress while executing the main form's Form_Load event. One method of doing this is by adding a label (or textbox) control to the splash screen form and updating it as parts of the application initalization start and finish.
Splash Screen form code changes
On the Splash Screen form you need to first create a delegate to be invoked later from the main thread.
Delegate Sub UpdateStatusLabel()
Next, create a Sub in the Splash Screen form's code that takes no input parameters but updates your status label. This may not make sense and seem redundant now, but it will once we get into BeginInvoke().
Public Sub UpdateLabelText1()
Dim msg As String = "Getting Computers from Active Directory..."
Me.lblStatus.Text = msg
End Sub
Public Sub UpdateLabelText2()
Dim msg As String = "Getting Users from Active Directory..."
Me.lblStatus.Text = msg
End Sub
Code to put in Form_Load event on main form
Next, we need to call the code from our Form_Load event (or another sub or function called by Form_Load).
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyForm.Load
...Do Something
We'll start by checking if the Spash Screen is on the screen
Next we call BeginInvoke on the Splash Screen object and store its outpuit in an IAsyncResult object
BeginInvoke takes two parameters: delegate and method, specified in the following format:
Delegate(AddressOf Method)
If My.Application.SplashScreen.Visible = True Then
Dim aResult As IAsyncResult = My.Application.SplashScreen.BeginInvoke( _
New SplashScreen.UpdateStatusLabel( _
AddressOf SplashScreen.UpdateLabelText1))
End If
...Do Something Else
End Sub
More
Thats all there is to it. If you're interested in more advanced info on BeginInvoke and its companion function EndInvoke it can be found here ( http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begininvoke(VS.80).aspx ) in the MSDN library. Here's another example that checks for a return value from the cross-thread operation (BeginInvoke):
If My.Application.SplashScreen.Visible = True Then
Dim aResult As IAsyncResult = My.Application.SplashScreen.BeginInvoke( _
New SplashScreen.UpdateStatusLabel( _
AddressOf SplashScreen.UpdateLabelText1))
Dim callResult As Integer = 0
callResult = My.Application.SplashScreen.EndInvoke(aResult)
'call result should be 0 (NERR_SUCCESS)?
End If