0

I doing some WPF application and today i faced some troubles. I need to show splash screen while initializing MainWindow. My code is below:

public MainWindowView()
{
    OnLoad();
}

public void OnLoad()
{
    worker = new BackgroundWorker();
    lw = new WaitWindowView();
    lw.Show();
    worker.DoWork += delegate(object s, DoWorkEventArgs args)
    {
        Dispatcher.Invoke(new Action(delegate()
        {
            InitializeComponent();
            DataContext = new NavBarVM();
        }), DispatcherPriority.Background);
    };
    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
    {
        lw.Close();
    };
    worker.RunWorkerAsync();
}

Above code is working. But splash screes is lagging.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
Edmon
  • 108
  • 2
  • 5
  • 2
    by lagging do u mean, the controls (buttons / whatever) do not respond to mouse inputs? or there is an animation inside the splash screen? – kevin Jun 26 '14 at 05:21
  • animation inside splash screen is lagging – Edmon Jun 26 '14 at 05:52
  • I think main window and splash screen must be separated. In my case splash screen not continually working. Little stop and then animation again little stop and again animation etc. – Edmon Jun 26 '14 at 06:01

1 Answers1

2

Your call to Dispatcher.Invoke will make the code in the DoWork handler run on the UI thread, so using a BackgroundThread like that is pointless. Furthermore, you can't call InitializeComponent on a background thread either, so that will not speed things up.

The normal way to display a splash screen can be found in my answer to the How to open a child Window like a splash screen before MainWindow in WPF? question, here on Stack Overflow. If you have a lot of initialisation code (you, not the Framework), then you can do that in the splash screen Window and pass the loaded objects back to the MainWindow before closing the splash screen.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183