Tony Vitabile
Tony Vitabile

Reputation: 8594

How do I make the main window display when I want it to during my initialization sequence

I have a WPF application I'm building. The program displays a splash screen as it initializes. Most of the startup sequence needs to be done in a background thread after the main window is displayed. My problem is that the main window doesn't display when I want it to.

Here is the Window_Loaded event handler for my main window:

private void Window_Loaded( object sender, RoutedEventArgs e ) {
    if ( !DoInitialize() ) {
        shuttingDown = true;
        Application.Shutdown();
    }

    e.Handled = true;
}

private bool DoInitialize() {
    if ( !ReadConfiguration( Application.ConfigurationFilePath ) ) {
        return false;
    }

    Thread th = new Thread( new ThreadStart( FinishInitializing ) );
    th.SetApartmentState(ApartmentState.STA); 
    th.Start();

    ClockTimer = new DispatcherTimer( TimeSpan.FromSeconds( 1 ), DispatcherPriority.Background, UpdateClock, Dispatcher );
    ClockTimer.Start();

    return true;
}

As you can see, except for reading the configuraton file, it doesn't do much except start the background Thread to finish the initialization and start a DispatcherTimer. This timer is used to update a clock on the display.

What is it exactly that triggers the display of the main window?

Tony

Upvotes: 0

Views: 237

Answers (1)

jimmyjambles
jimmyjambles

Reputation: 1670

If you wish to execute code after the window has been rendered (shown) you can attach to the Window.ContentRendered event on your Main Window.

To answer your question the order is

  • SplashScreen shown
  • MainWindow Constructor (InitializeComponent())
  • Window.Loaded called
  • Window shown
  • Window.ContentRendered called
  • SplashScreen hidden

Upvotes: 1

Related Questions