1
votes

Background:

I'm trying to add an animation to a splash screen window in my WPF project. (Not using the actual SplashScreen control. I use a window.)

My reason for implementing the splash screen is beacuse there's a slight delay when the application is opened, due to the loading of data. Obviously, the splash screen's purpose is to aid the user experience, so the user knows the application has been executed.

Here's the idea behind my current approach:

  1. Instantiate the splashscreen window at the beginning of the constructor.
  2. InitializeComponent() is called.
  3. Close the splashscreen window.

Unfortunately, the animation in the splash screen doesn't start until after the InitializeComponent() method of the the main parent window is called; this occurs basically when I'd WANT TO close the splashscreen window anyway. Hence the animation is useless and doesn't start until it's too late.

My question:

  1. Why does it not animate until after InitializeComponent() is called?

  2. My current theory for why this happens is that it's because the splash screen is being opened on the main UI thread. Am I correct?

  3. Are there any workarounds that don't involve hijacking control of the program and spawning the splashscreen in a new thread? (I don't want a solution that hackish)

And yes, I did browse this site for quite awhile and was unable to find a viable solution.

2
Why do you consider creating the Splash screen in a different thread a hack? it is not. It is actually the only solution you have, when your UI is heavy and it takes time to load.Federico Berasategui
Anyways, you need to make sure that you're loading your Data in a background thread first, that might alleviate the UI thread and allow you to display the splash screen for a while until data is ready, at least.Federico Berasategui
The reason it does not animate is that the UI thread is busy doing your stuff, and has no time for animations. Free the UI thread or create the Splash screen in a different thread.Federico Berasategui

2 Answers

1
votes

Processing time on the UI thread blocks the animation. No real way around it.

The best approach is to NOT spend so much time loading and blocking on the UI thread. I suggest that you could delay the loading of the data bound to your UI elements until after the initial load. That way, the InitializeComponents() should not take so long.

Basically, all your bindings should return no data to the controls until AFTER you initialize. This data initialization would preferably happen on a separate thread, and then notification would occur after the data has been loaded.

This would allow your splash screen to animate and you will have much more joy.

1
votes

There are a few approaches that I've taken to handle this in the past, though @HighCore is right in the comments as well about your current problem

Regardless, these solutions boil down to threading, since the UI pipeline needs to render and you can't inherently do these things on the UI thread without blocking, particularly if you want animation.