2013-01-02

Have More Control Over WPF Splash Screen Timings

I just discovered a little trick on WPF splash screens. Normally, you start a WPF splash screen like this:

public partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        SplashScreen splash = new SplashScreen(@"Images\splash.bmp");
        splash.Show(false, true);
        splash.Close(new System.TimeSpan(0, 0, 5));
        base.OnStartup(e);
    }
}

The problem is that the splash screen starts fading immediately. If you only give it two or three seconds, by the time the brain starts processing that it is there, it has faded to being unreadable. So to keep it readable, you need about five seconds, which is longer than I'd like it to be up.
What I'd like is to make it stay solid for 1.5 seconds, then fade out over 1.5 seconds. Here's how to do it:

public partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        SplashScreen splash = new SplashScreen(@"Images\splash.bmp");
        splash.Show(false, true);
        splash.Close(new System.TimeSpan(0, 0, 3));
        System.Threading.Thread.Sleep(1500);
        base.OnStartup(e);
    }
}

The Sleep causes the thread to go to sleep for 1.5 seconds. When the thread wakes up, it only has 1.5 seconds to get rid of the splash screen to meet the requirements of the TimeSpan. Perfect! It does have the drawback that the app is doing nothing on the main thread for 1.5 seconds. If your app is complex, you might need a shorter amount of sleep, or spawn another thread.

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.