WPF splash screen with own thread gets dug behind other windows
At start of my WPF application I have to load some data. This can take some seco开发者_开发百科nds, so I built a splash screen with a text progress text and a bar. The screen is run by its own thread:
public static class SplashScreen
{
public static void ShowSplash()
{
if (_SplashThread == null)
{
_SplashThread = new Thread(() =>
{
_Splash = new WISplash();
_Splash.Show();
_Splash.Closed += (sender1, e1) => _Splash.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
_SplashThread.SetApartmentState(ApartmentState.STA);
_SplashThread.IsBackground = true;
_SplashThread.Start();
}
}
}
WISplash
's base class is Window
.
Edit: I have to state more precisely: My splash screen does not have the same task bar button. In fact, it has no button at all. I've uploaded a basic project to demonstrate what's happening: MyApp.zip
Is there a possibility to give the splash screen the same task bar button as the main window?
I would suggest not giving the splash window any taskbar button. When your app gets activated (with event Application.Current.Activated) you can do this: 1) Make the splash window visible 2) Make the splash window topmost (temporarily) 3) Wait for a short time (a few milliseconds) 4) Make the splash window non-topmost This will make the splash window pop in front of all other windows on activation.
When your app gets deactivated (with events Application.Current.Deactivated or Application.Curent.Exit), make the splash window hidden.
I think that should give you the behavior you are looking for.
精彩评论