Splash screens...and background processing...

I start my app from sub main, and display a splash screen during startup.
The splash screen was developed with a 5 second timer, however I'm not liking the results.
When my main form loads, I have a bit of processing occurring that I'd like to mitigate if possible and have that happen while the splash screen is displayed.
I'm hoping I haven't headed too far that this is irreversible :^), but how can I have that main form load and do its thing, while the splash is displaying, so that there seems to be less 'setup' time than there really is...??

The splash's load starts the 5 second timer, and once the timer is set, I simply take the first tick to hide the form...
    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)  'hide splash after 5 seconds
        Me.Hide()

Hopefully there's an easy way to 'launch' the main form's load code, while this timer is counting down??
LVL 67
sirbountyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
What version VB.Net sirbounty?

Also, show us your Sub Main() please...
sirbountyAuthor Commented:
2005 sir.
Here's the main:
Sub Main()
  Dim ShowSplash As String = cfg.GetSetting("ShowSplash")
  Application.VisualStyleState = VisualStyles.VisualStyleState.ClientAndNonClientAreasEnabled
  Application.DoEvents()
  If ShowSplash <> "Off" Then frmSplash.ShowDialog()
  Dim strDept As String = cfg.GetSetting("Department")
  If IsNothing(strDept) Then
    strDept = QueryDept(UserName.Substring(UserName.Length - 6))
    cfg.SaveSetting("Department", strDept)
  End If
End Sub

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
2005 huh?...

Try this first (before we go mucking it up)...

Click on Project Properties, then in the Application Tab, set the "Splash Screen:" dropdown to "frmSplash".
Now DISABLE the Timer in frmSplash.
Then temporarily change your Sub Main() to the below.

Run it...the splash screen is SUPPOSED to stay visible while your MAIN form loads and then automatically be closed.

Did it work?...  If not, we'll try a different approach...

That might not work at all now that I think about it.  You are using Sub Main() with 2005 which means you have turned OFF the Application Framework.

You should turn that back on by checking the "Enable Application Framework" box in the Project Properties.  The QueryDept() code can be placed in the Application Startup() event:

To get the Startup() event:
Click on Project --> Properties.
On the Application Tab, make sure the "Enable Application Framework" check is CHECKED.
Then click on the "View Application Events" button in the bottom right of that Tab.
Now in the top of the Code window are two DropDown lists.
Change the Left DropDown from "(General)" to "(MyApplication Events)".
This should give you this in your window:

Namespace My

    ' The following events are availble for MyApplication:
    '
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication


        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

        End Sub

    End Class

End Namespace

Sub Main()
  Dim ShowSplash As String = cfg.GetSetting("ShowSplash")
  Application.VisualStyleState = VisualStyles.VisualStyleState.ClientAndNonClientAreasEnabled
  Application.DoEvents()
  ' If ShowSplash <> "Off" Then frmSplash.ShowDialog()
  Dim strDept As String = cfg.GetSetting("Department")
  If IsNothing(strDept) Then
    strDept = QueryDept(UserName.Substring(UserName.Length - 6))
    cfg.SaveSetting("Department", strDept)
  End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

sirbountyAuthor Commented:
Ah...this might be the reason why I didn't use that approach.

I currently support two seperate vb6 apps that do basically the same thing.
I was going for a one-and-done approach here.
That strDept will be one of two departments that each require a slightly different approach, thus I was using the frmSplash code to determine which of the two main forms would be the 'actual' main form...
Thus, enable application framework isn't even checked in my app, since I'd have to specify an actual main form...

Does that alter this too much to make something like what I'd like possible?
Thanx very much for your time here!
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's possible...and even easy in VB.Net 2005...

You can change the startup form in the Startup() event:

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim strDept As String = cfg.GetSetting("Department")
            If IsNothing(strDept) Then
                strDept = QueryDept(UserName.Substring(UserName.Length - 6))
                cfg.SaveSetting("Department", strDept)
            End If

            ' make some decision based on "strDept" (I assume this is what you want!)
            If someCondition Then
                Application.MainForm = New someFormNameHere
            Else
                Application.MainForm = New someOtherFormNameHere
            End If
        End Sub

If you don't change "Application.MainForm", then the normal form specified as the "Startup Object" is loaded as normal.
sirbountyAuthor Commented:
Was out of town this weekend...but I like that this sounds easy.
For this, I would still go the route you suggested before, by reenabling app framework, setting my frmSplash as the splash and then 'what' as the main form?

Then the above code would go on 'both' main forms?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Re-enable the Application Framework.
Set the Splash dropdown to frmSplash.
Set the Startup dropdown to ANY form as it doesn't matter.
    We are setting the main form via code at run-time anyways!

The code goes in the STARTUP() event:
    Project --> Properties --> "View Application Events" button (in the bottom right).
    Now in the top of the Code window are two DropDown lists...
    Change the Left DropDown to "(MyApplication Events)".
    Change the Right DropDown to "Startup".

Now you can place code into the Startup() event, which occurs at the Application level, BEFORE any forms are loaded:
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim strDept As String = cfg.GetSetting("Department")
            If IsNothing(strDept) Then
                strDept = QueryDept(UserName.Substring(UserName.Length - 6))
                cfg.SaveSetting("Department", strDept)
            End If
 
            ' make some decision based on "strDept" (I assume this is what you want!)
            If someCondition Then
                Application.MainForm = New someFormNameHere
            Else
                Application.MainForm = New someOtherFormNameHere
            End If
        End Sub

Open in new window

sirbountyAuthor Commented:
Nearly there...it works as it's supposed to, but the splash stays up even after the form has finished loading.
Also - will I be able to still offer the option to not display the splash?  Not a big deal, if not - but before, I had a checkbox to disable it, if the user wanted...

Thanx.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Do you have any code in your splash form?...that may be interfering with it being closed automatically...

I thought you could simply do this in the StartUp() event to have NO splash screen:

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Application.SplashScreen = Nothing
        End Sub

But I'm finding that this causes the application to not close properly...   =\
sirbountyAuthor Commented:
This is all I have for form code on the splash form...
Private Sub frmSplash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  If My.Application.Info.Title <> "" Then
    ApplicationTitle.Text = My.Application.Info.Title
  Else
    ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
  End If
 
  'Version info
  Version.Text = System.String.Format(Version.Text & " " & My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor)
  
  'Copyright info
  Copyright.Text = My.Application.Info.Copyright
End Sub
 
Private Sub chkShowScreen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkShowScreen.Click
  cfg.SaveSetting("ShowSplash", "Off")
End Sub

Open in new window

sirbountyAuthor Commented:
Bummer - seems to work if remove these two lines:

Application.MainForm = New someFormNameHere
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You need to replace the "someFormNameHere" with the name of YOUR form:

    Application.MainForm = New someFormNameHere

The code is showing how you can make a decision based on ~something~ and then make your Main Form based on that:

            If ZZZ Then
                Application.MainForm = New XXX
            Else
                Application.MainForm = New YYY
            End If

In the above snippet...YOU need to replace:

    "ZZZ" with some kind of conditional check such as
        If strDept = "Idle_Mind" Then

    "XXX" with some form name such as frmPrimary

    "YYY" with some other form name such as frmSecondary

I don't know EXACTLY how to integrate it into your code bud...   =\

Work with me here!   =)

sirbountyAuthor Commented:
LOL - come on bud - give me a 'little' credit...
Of course I replaced that with 'my' code...
Here's what I have:
and it's not until I remark the line(s) below that the splash disappears on it's own...
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
Dim strDept As String = cfg.GetSetting("Department")
  If IsNothing(strDept) Then
    strDept = QueryDept(UserName.Substring(UserName.Length - 6))
    cfg.SaveSetting("Department", strDept)
  End If
 
  Select Case strDept
    Case "One Department"
      ' Application.MainForm = frmNumberOne (this one's 'mine'), and also the one listed as the actual startup form - so commenting it out allows everything to run as expected
    Case "The other Department"
      Application.MainForm = frmNumberTwo 
  End Select
End Sub

Open in new window

sirbountyAuthor Commented:
Well, nothing I've tried is working...
I've removed all the frmSplash code, tried using Application.HideSplash, splashscreen.hide/.close (cross-thread operation is not valid).
Got any ideas?
sirbountyAuthor Commented:
I've got a rather ugly approach at dropping a timer back on my splash and then closing it after 5 seconds.
It "works" but certainly not the way it's supposed to.  I was more hopeful for a way to determine that the main form had completely loaded before closing it...
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"I was more hopeful for a way to determine that the main form had completely loaded before closing it..."

The form's Shown() event is ~supposed~ to fire after the form is, well...shown.   ;)

I'll fiddle with it a bit more to see if I can figure out where we have gone wrong.
sirbountyAuthor Commented:
Even that would work, if I could somehow reference the frmSplash...if I try to close it in the shown event, I get:

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'frmSplash' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"

when using
frmSplash.Close
sirbountyAuthor Commented:
Well, if we can't get anything resolved on this by Monday, I'll go ahead and close it.
Right now, I just forego changing the main form, since I'm in the department that the startup form is set to.
But I may need a more elegant solution once I start testing with the other form...could be a while - if ever...
Thanx, as always.
sirbountyAuthor Commented:
Hey mate - I didn't actually have this working until after I closed it - but you certainly got me on the right track.
However...it IS working now.
I basically moved the code from app startup over to the splash form.
Anyway - thought you'd like to know.  Thanx again for the excellent help! :^)
  Private Sub frmSplash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim strDept As String = cfg.GetSetting("Department")
            If IsNothing(strDept) Then
                strDept = QueryDept(UserName.Substring(UserName.Length - 6))
                cfg.SaveSetting("Department", strDept)
            End If
            If strDept = "Dept#1" Then My.Application.ApplicationContext.MainForm = New frmTSS
            If strDept = "Dept#2" Then My.Application.ApplicationContext.MainForm = New frmES
 
            If cfg.GetSetting("ShowSplash") Is Nothing Then cfg.SaveSetting("ShowSplash", "True")
            If cfg.GetSetting("ShowSplash") = "False" Then
                Me.Close()
                Exit Sub
            End If

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.