My 00000010 cents

Ponderings of a Runtime Dev

Splash Screens in VB 2005

One of the things we provided in the application model for VB is the ability to easily integrate a splash screen into your application.  We take care of displaying it, making sure it displays for at least some user specifiable period of time, and taking it down once your application's main form is ready to display.

One of the things that throws people is how to set the minimum amount of time for the splash screen to display.  Admittedly, we have some work to do to make this easier. 

For now, if you don't like the default two second minimum, you can specify your own minimum in milliseconds by overriding the OnInitialize method on the MyApplication class.  Here's how you do it (and note that all of this presupposes that you have the application model enabled in the first place):

Go to the project designer by double-clicking the 'My Project' node in your solution.  Once you've specified which form is to be your splash screen, click the View Application Events button.  You'll see a MyApplication class inside the My namespace.  Go in that class and add an override to OnInitiliaze like this:

Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
  Me.MinimumSplashScreenDisplayTime = 5000
  Return MyBase.OnInitialize(commandLineArgs)
End Function

Now the splash screen will stay up a minimum of five seconds.

The splash screen displays early on in your applications startup.  The idea is to have that displaying until your main form is ready to display.  Then we take it down unless you've specified a minimum 'face time' that you want for your splash screen.  Once the minimum splash time expires we take the splash screen down.  Just set it to zero if you want it taken down automatically once your main form has completed loading.

All of this exposes you to a variety of things going on behind the scenes.  Next time I'll write about the application model and what the sequence of events is when an app that uses the new application model starts up.

Published Wednesday, February 01, 2006 6:09 PM by Tyler Whitney

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

bcjmk said:

I was playing around with a splash screen for my application. It was supposed to read from the my.application object to identify version and update the splash screen. Once I deployed the application I kept getting untraceable null object exceptions. After reading your blog and realizing that the splash screen must be an asynchronous event, I commented out the code that updated the text block et voila! no more null object exceptions.

OTOH - I did kill off a lot of potential defects whilst hunting for the little bugger....

It would be a REALLY nice addition to have the timer capability or some other capability to allow splash screens to be built and updated to include product or setting information programmatically.

Thanks for the tip.
February 14, 2006 1:05 PM
 

zach davis said:

very helpfull article.  Really shows some of the functionality for vs2005

thanks

December 6, 2006 1:04 PM
 

Corey Beck said:

I tried what he said in this article. When playing around i found that if you put a module to save a setting and inside the splash screen like

Friend Module MyConfig

Private __splashLoading As Boolean = True

Public Property isSplashLoadingDone As Boolean

Get

  Return __splashLoading

End Get

Set(ByVal value As Boolean)

 __splashLoading = value

End Set

End Property

End Module

Then inside your loading form under the load event use a loop  to loop tioll your loading procedures inside

your splash screen finish like this

after your loading procedures are done

My.MyConfig.isSplashLoading = False

In your form put

Do While My.MyConfig.isSplashLoading = True

Loop

If that will be any help to anybody

April 7, 2007 9:45 PM
 

stumped said:

My splashscreen won't close.  I've tried adjusting the timing as suggested and a frm.close produces an error.  I've had this working on an XP box but now for some reason it wont work on win 2000, does anyolne have any idea shy I am having this seemingly ridiculous problem?

ps

April 13, 2007 2:15 PM
 

stumped II said:

I should add that even after the startup form is asked to close the splash screen still doesn't want to close. -- so I have to manually terminate the program

April 13, 2007 2:18 PM
 

Tyler said:

Regarding the problems with closing the form yourself:  The splash screen form runs on another thread so it doesn't block the main thread as your app initializes.  But this means you can't just call form.close on it.  

You may want something like this:

                   Dim TheBigGoodbye As New DisposeDelegate(AddressOf SplashScreen.Dispose)

                   SplashScreen.Invoke(TheBigGoodbye)

                   SplashScreen = Nothing

I'm curious why you are manually closing down the splash screen yourself instead of letting the application infrastructure do it?  I am crazy busy at the moment but should be able to look at this if you have more details next week.

April 13, 2007 4:20 PM
 

stumped III said:

Thanks for your quick reply.

The reason I am trying to close the form myself is because it won't close itself.  Even after I have closed the main (startup) form (clicking the x) myself the splash won't close...

Peter

April 16, 2007 4:03 PM
 

stumped IIII said:

Do you think there could be a connection to windows 2000?

April 25, 2007 3:17 PM
 

Tyler Whitney said:

Mysterious.  No, this works just fine on Windows 2000.  I just gave it a whirl.

You are following the instructions from my post, right?  There is no funky shutdown code in your splash screen that is stopping it from closing?  Does your main form or splash form have any interesting code running when it loads?

May 1, 2007 1:55 PM
 

Stefano said:

I'm fighting with a problem with my splashscreen:

when the application starts, it shows the splash, then it closes and the main form shows.... ok !

But the mainform is not the active window. It doesn't have the focus.

How to solve it ? I'm thinking not to use the My.Splashcreen feature anymore...

August 2, 2007 4:20 AM
 

Tyler Whitney said:

Sorry for the delay.  I've been out of the office for awhile.

There is a way around this, but it is really rather involved.  I will try to write up how to do it and post it in the next few days.

What currently happens is that after the splash screen is closed we make a call to set the main form to Active, which is what doesn't work in your scenario.  Unfortunately there isn’t an easy way to override this behavior.  With some gymnastics it can be done, but it isn't pretty.

I've entered a work item for myself to consider for the next release that will make this case much easier.  I can't guarantee that we'll do it (it always depends on schedule pressure and what else is going on for a release) but I'd like to get to it.

August 7, 2007 4:41 PM
 

Stefano said:

Thank you Tyler !

I found out that if I click with mouse on the Splash screen, when it shows up, after then the Mainform gets the focus...

August 8, 2007 4:34 AM
 

Stefano said:

I finally resolved this problem with this (don't know why, delay time I suppose) :

Protected Overloads Overrides Sub OnCreateSplashScreen()

Me.SplashScreen = My.Forms.FormSplash

For i As Integer = 0 To 100

My.Forms.FormSplash.Opacity = i

Next

End Sub

August 8, 2007 3:52 PM
 

Imran said:

I have a rather ridiculous problem ... while my splash screen is being displayed, its is supposed to be reading file extensions from the registry, it goes on fine for a few seconds, but then the main form shows up before the process is finished and the splash screen disappears. I have tried after making the minimum time higher but that doesn't do any good either ... Is there anything wrong in my code or something? Here is the code in the splash screen:

Public NotInheritable Class splash

   Dim reg(100000) As String

   Private Sub splash_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

       My.Computer.Registry.ClassesRoot.GetSubKeyNames.CopyTo(reg, 0)

       My.Application.DoEvents()

       Dim i As Integer

       Dim count As Integer = 0

       For i = 0 To 100000

           If reg(i) = "" Then

               Exit For

           Else

               If reg(i).Chars(0) = "." Then

                   lab.Text = reg(i)

                   count += 1

                   My.Application.DoEvents()

               End If

           End If

       Next

       MsgBox(count)

   End Sub

End Class

___________________________________________________

- "lab" is the label on the splash screen, acting as a feedback ...

- variable "count" was used while i was finding the error

- file extensions are stored in HKEY_CLASSES_ROOT in the registry

-------------------------------------------------------------------

Thanks for reading this,

Imran

September 8, 2007 1:41 PM
 

mike2008 said:

is it possible to display the splash screen until MainForm's OnLoad method finishes?

it looks like the splash form is closed or hide before OnLoad method.

Thanks.

July 24, 2008 10:02 AM
 

Tyler Whitney said:

Hi Mike,

I've been quite negligent on this blog.  I switched to the compiler team awhile ago and have been working pretty hard on removing explicit line continuation from the language.  I'll try to get back on the wagon here with regards to splash screen questions.

The application framework actually hooks the main form's Load event and doesn't attempt to close the splash screen until OnLoad is done.  Even if the splash screen timeout occurs before form load completes, the splash screen should stay up.

Are you using the default method of setting up a splash screen via the app designer?  I'd be interested in how you have this configured to see if there is a problem on our end that needs to be addressed.

July 30, 2008 2:26 PM
 

Tyler Whitney said:

Hi Imran,

Two variables govern how long the splash screen stays up.  If no minimum time-out for the splash screen is specified, the splash closes when the startup form's Load event (e.g. when Form1_Load() handles mybase.Load) returns.

If you do specifiy a minimum time-out (see post above on how to do that), then the splash closes when two things are true: the startup form's load event has returned AND the timeout has expired.

But there may be a better way than trying to arrive at a good delay time for the splash screen.  What might work is to put your file extension loading code in the startup form's load handler (e.g. Form1_Load()... ) so that the splash screen doesn't close until the main form's load is finished.

There are quite a number of things I'd like to do to the Splash screen plumbing to make things like this easier in the future.  I'll have to see if I can do any moonlighting on the runtime during the next cycle that they are doing work in this area.

July 30, 2008 2:59 PM
 

My 00000010 cents Splash Screens in VB 2005 | Wood TV Stand said:

June 1, 2009 1:48 PM
 

My 00000010 cents Splash Screens in VB 2005 | Wood TV Stand said:

June 2, 2009 8:21 PM
 

My 00000010 cents Splash Screens in VB 2005 | debt consolidator said:

June 19, 2009 10:49 AM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker