user844541
user844541

Reputation: 2958

animation won't start until onStartup is finished

I am developing a WPF, C# application.

Because the load process is very long, we needed to create an animated splash screen to be shown while the application is loading.

The problem is that all the intialization code is in the "OnStartup" method. I notice that the animation won't start until OnStartup is finished, so I guessed that you cannot show any animation before the application startup is ended.

Am I right with this assumption? if yes, is there a way to show animation before startup is done, or should I move the code to another place? (I prefer not to do that because this will be a huge change in the application flow).

EDIT

I tried to use a new thread and to start the splash using the dispatcher, but the invoke of the dispatcher didn't start until the OnStartup was done.

Upvotes: 1

Views: 427

Answers (2)

jrb
jrb

Reputation: 1728

Create a new window with the animation. Start this window first and when its up create the main window and close the first one once the main is loaded.. You can set the animations window to "always on top" to prevent the main window from covering the animation.

Upvotes: 1

Louis Kottmann
Louis Kottmann

Reputation: 16618

You should do:

protected override void OnStartup(StartupEventArgs e)
{
    //Start your animation here.

    Task.Factory.StartNew(() => 
    {
       //Start long loading operation here

       Application.Current.Dispatcher.Invoke(
          new Action(() => 
             { 
                //Stop your animation here
                //Note: it needs to run on the UI thread, so we dispatch it.
             })
       );
    }
}

Upvotes: 4

Related Questions