MessageBox with exception details immediately disappears if use splash screen in WPF 4.0

The reason lies in how the SplashScreen uses BeginInvoke to Close itself. I couldn't pin down exactly where the MessageBox is getting closed*, but I did see a simple fix:

Don't use MessageBox.

Create an error window, let's call it "ErrorWindow.xaml". Use that window to display the error message to the user and respond to the OK button.

Follow this guideline to declare your own Main procedure and alter it like so:

Edited to show how you might pass info to the ErrorWindow.

public static void Main()
{
    SplashScreen splashScreen = new SplashScreen("whatever.jpg");
    splashScreen.Show(true);
    string errorMessage;
    bool dataLoaded = LoadDataFromDatabase(out errorMessage);
    WpfApplication1.App app = new WpfApplication1.App();
    Window windowToRun = dataLoaded ? (Window)new MainWindow() : (Window)new ErrorWindow { ErrorMessage = errorMessage };
    app.Run(windowToRun);
}
  • My guess is that SplashScreen.Show and Application.Run are two separate message pumps. The first is terminated with a call to PostQuitMessage. That explains why the MessageBox closes.

On a similar StackOverflow question, I listed several different approaches for dealing with this problem.

You might find some of these other tricks useful if @Tergiver's approach doesn't work for your application.

how to set wpf MessageBox.Owner to desktop window because SplashScreen closes MessageBox