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

大憨熊 提交于 2019-12-23 07:38:35

问题


My desktop-based WPF-application (4.0) works with DB and in order to this it should establish a connection with SQL Server on application's startup. Of course, this operation takes some time and user have to wait some seconds (3-5) while .Net Framework start and connect to SQL Server.

As appropriate in such kind of cases I decided to use a splash screen. I added some picture to the solution, set build action as «Splash screen», compiled my application, it works! If the attempt to connect to SQL Server failed (e.g. server is not available) my application throws an exception and I show to user MessageBox with warning and exception details, user press OK and application shutdowns (Application.Current.Shutdown()).

Before I added splash screen all this logic used to work perfectly, but now, with splash screen added, if I run application while SQL Server is not available, application throws an exception (as I asked in my code), but MessageBox with notification appears for 1-2 seconds and disappear without any user interaction, user even can't read what is written on it.

I discovered, that if I try to show 2 MessagBoxes, so the first one will appear and disappear immediately, but the second one will stay until the user will press OK.

My question is: How to solve this issue? I want to use splash screen and show one MessageBox if an exception has been thrown and let the user to decide when to close it (user click on the OK button).

Here is a flow chart describes logic of my application:

No exception (good scenario): Run app → Splash screen → if(isConnectedToSQL=true) → Show main window…

With exception (bad scenario): Run app → Splash screen → if(isConnectedToSQL=false) → Throw exception → Show MessageBox with exception details → User click on OK → Close application.


回答1:


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.



回答2:


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




回答3:


Given the problem description at https://connect.microsoft.com/VisualStudio/feedback/details/381980/wpf-splashscreen-closes-messagebox#tabs one way to solve this is to DllImport SetActiveWindow and call it as SetActiveWindow(IntPtr.Zero) just before calling MessageBox.Show. Then the message box won't get the splash screen as its parent and won't die when the splash screen closes itself.



来源:https://stackoverflow.com/questions/3891719/messagebox-with-exception-details-immediately-disappears-if-use-splash-screen-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!