Click here to Skip to main content
Click here to Skip to main content

WPF Splash Screen With Minimum Duration

, 24 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to make a splash screen appear for a minimum duration with very little code

Introduction

Splash screens are easy to add to WPF applications, but allow for little flexibility. In particular, you cannot specify a minimum duration that a splash screen can appear for. Such a minimum duration can be desirable to ensure users on faster and slower PCs have a similar loading experience. A minimum duration can also ensure that users on fast PCs do not simply see the screen flash as the splash appears and vanishes. This tip shows how to simply and cleanly show a SplashScreen for a minimum period of time, without resorting to sleeping threads or other hackish solutions.  

Using the Code

To specify a minimum showing duration, we inherit from the SplashScreen class and provide a new Show() method:

public class SplashScreen2 : SplashScreen
{
    private DispatcherTimer dt;
    
    public SplashScreen2(string resourceName)
        : base(resourceName)
    { }
    
    /// <summary>
    /// Shows the splash screen
    /// </summary>
    /// <param name="minDur">The minimum duration this splash should show for</param>
    /// <param name="topmost">True if the splash screen should appear 
    /// top most in windows. Recommended to be true, to ensure it does not appear 
    behind the loaded main window</param>
    public void Show(TimeSpan minDur, bool topmost = true)
    {
        if (dt == null)//prevent calling twice
        {
            base.Show(false, topmost);
            dt = new DispatcherTimer(minDur, DispatcherPriority.Loaded, 
                        CloseAfterDelay, Dispatcher.CurrentDispatcher);
        }
    }
    
    private void CloseAfterDelay(object sender, EventArgs e)
    {
        dt.Stop();
        Close(TimeSpan.FromMilliseconds(300));
    }
}

The Show method above shows the SplashScreen but states that it will manually close itself. It then starts a new DispatcherTimer, which will call CloseAfterDelay when both:

  1. The timespan has completed AND
  2. The application has loaded.

To use the new SplashScreen2:

  1. Add an image to your WPF project. Under properties for your image, change 'Build Action' to 'Resource'. Do not set it to SplashScreen, because this will insert unwanted code into your Main[] method.
  2. In App.cs, create and show a new SplashScreen2 in the constructor, specifying the minimum duration you wish it to show for:
public App()
{
    SplashScreen2 splash = new SplashScreen2("my_splash_image.jpg");
    splash.Show(TimeSpan.FromSeconds(2));//Show for at least 2 seconds min duration
}

History

  • April 14 2013: Project submitted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lee Reid
Musink Limited
Australia Australia
Lee Reid is the creator of the music-composition software Musink. He is a PhD-candidate at the University of Queensland and CSIRO in the field of Neuroscience and Biomedical Imaging.

Comments and Discussions

 
GeneralNice, simple example. Pinmemberpolczym22-Nov-14 1:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Apr 2013
Article Copyright 2013 by Lee Reid
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid