8

i have a wpf application, i need to display a messagebox, the problem is that the message box is displayed for 0.5 second and doesn't even wait for user to click OK.

MainWindow.xaml.cs :

public partial class MainWindow : Window
{
    public MainWindow()
    {

        //verifying application setting file to see if the connection is ok
        string pathToApp = System.AppDomain.CurrentDomain.BaseDirectory + "settings.sts";
        ApplicationSettings applicationSettings = new ApplicationSettings();
        applicationSettings.ServerIp = "127.0.0.1";
        applicationSettings.ServerDatabase = "test";
        applicationSettings.ServerUserName = "root";
        applicationSettings.MakeConnectionString();
        foreach (char  c in "")
        {
            applicationSettings.ServerPassword.AppendChar(c);
        }



        MySqlConnection connection = new MySqlConnection(applicationSettings.ConnectionString);
        try
        {
            connection.Open();
        }
        catch (Exception e)
        {
            // here the message box shows for 0.5 second and closes immediately
            MessageBox.Show(e.Message);
        }
        finally
        {
            connection.Close();
        }

        //display window
        InitializeComponent();

    }

i should also that am using a image as a splash screen, if this have a relation with the message box.

sorry this code is not yet completed. thanks in advance

Redaa
  • 1,540
  • 2
  • 17
  • 28
  • Can you show the initialization code for the app? What is displaying your "splash screen"? It's possible that the splash screen closing is causing the app to close before you main window is considered "loaded". – Nathan A Jul 31 '14 at 17:06
  • @NathanA i only added an image and changed its build action to SplashScreen, – Redaa Jul 31 '14 at 17:12
  • @NathanA can you please help :/ – Redaa Jul 31 '14 at 17:20

2 Answers2

15

Your problem stems from a known issue with WPF:

First, it happens when used with the splash screen. If you don't specify an parent for a message box, it assumes the splash screen is it's parent and is therefore closed when the splash screen closes. Second, even if you specify the parent as the MainWindow while in MainWindow's constructor, it still won't work since MainWindow doesn't have a handle yet (it gets created later on).

So, the solution is to postpone the invocation of the message box until after the constructor, and by specifying MainWindow as the parent. Here is the code that fixes it:

Dispatcher.BeginInvoke(
    new Action(() => MessageBox.Show(this, e.Message)),
    DispatcherPriority.ApplicationIdle
);

Here's a reference to the parent/splash issue: http://connect.microsoft.com/VisualStudio/feedback/details/381980/wpf-splashscreen-closes-messagebox

Nathan A
  • 11,059
  • 4
  • 47
  • 63
  • 1
    it's working but one more little thing is that the message box is displayed before mainwindow shows, then closes after this the mainwindows shows up and the messagebox again. – Redaa Jul 31 '14 at 17:28
  • May I also suggest that you take out all your code from the constructor and maybe handle it in the "Loaded" event? A lot of these issues may go away if you do that. Constructors should be left for instance construction work, not displaying of message boxes or other heavy-duty work. – Nathan A Jul 31 '14 at 17:39
1

In my situation (minimal tray icon app) there was no splash screen or MainWindow.

I found this solution from another SO question helpful:

var tempWindow = new Window();
var helper = new WindowInteropHelper(tempWindow);
helper.EnsureHandle();
MessageBox.Show(tempWindow, "Lorem Ipsum");
tempWindow.Close();

(I am not sure if the .Close() can be omitted)

Martin Schneider
  • 14,263
  • 7
  • 55
  • 58