Home > Software design >  How to execute code before the splash screen is displayed?
How to execute code before the splash screen is displayed?

Time:07-05

In my app project I use this to display my splash screen image:

<SplashScreen Include="Images\SplashScreen.png" />

I also use a Mutex to prevent the application being opened more than once. However, even with the Mutex check in the App class constructor the splash screen is displayed before the Mutex check is performed.

Is there some way I can perform the check before the splash screen is displayed to prevent the user seeing it on the second instance?

CodePudding user response:

You can define and show the splash screen programatically, which allows you to control when it is shown and what happens before and after. Add your image with BuildAction set to Resource.

public partial class App : Application
{
   protected override void OnStartup(object sender, StartupEventArgs e)
   {
      // ...execute pre action here.
   
      SplashScreen splashScreen = new SplashScreen(@"Images\SplashScreen.png");
      splashScreen.Show(true);

      base.OnStartup(e);
      // ...other code and post action.
   }
}

See the SplashScreen type for more info and please be aware that there two overloads for the Show method. The parameter determines, whether the spalsh screen is automatically closed after the application was loaded. You could pass false and use the Close method to determine yourself when it is closed and execute your action afterwards.

  • Related