Solved

Splash screens...and background processing...

Posted on 2007-11-30
19
622 Views
Last Modified: 2008-02-01
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??
0
Comment
Question by:sirbounty
  • 12
  • 7
19 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20384242
What version VB.Net sirbounty?

Also, show us your Sub Main() please...
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20384381
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

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20384525
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

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20384701
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!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20385066
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.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20393458
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?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20393750
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

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20395017
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20396171
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...   =\
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 67

Author Comment

by:sirbounty
ID: 20397697
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

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20398363
Bummer - seems to work if remove these two lines:

Application.MainForm = New someFormNameHere
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20399407
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!   =)

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20399774
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

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20400448
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?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20410792
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...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20411929
"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.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20413112
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
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20429472
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.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20464264
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

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now