Message box closes automatically after a brief delay

Redaa

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

Nathan A

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Enable Macros after message box closes

From Dev

Fancy Box 2 automatically closes divs?

From Dev

SaveFileDialog closes automatically directly after calling showDialog()

From Dev

Way to automatically change fragments after delay

From Dev

Close dialog box after a message

From Dev

Ending a subroutine after a message box

From Dev

Dropdown field automatically closes after opening in IE11

From Dev

java joptionpane which automatically closes after few seconds

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

go to previous window automatically after printing dialog closes

From Dev

java joptionpane which automatically closes after few seconds

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

Dropdown field automatically closes after opening in IE11

From Dev

Dialog box closes slowly after clicking send button

From Dev

Application closes automatically

From Dev

Putty - Automatically closes on authentication

From Dev

NSPopover closes automatically

From Dev

Putty - Automatically closes on authentication

From Dev

Displaying a message box after a certain period of time

From Dev

Show a message box after usercontrol fully displayed

From Dev

Show Message Box After Button Click

From Dev

Value in cell is changing after a message box

From Dev

How to automatically Click a Button in Android after a 5 second delay

From Dev

show input dialog box after error message dialog box

From Dev

Spring Tool Suite Closes Automatically after adding dependency to pom.xml file

From Dev

After Showing Alert box subview button is automatically hidden in swift

From Dev

Widget opens for a second then automatically closes?

From Dev

FCM push notification closes automatically

From Dev

custom jquery alert closes automatically

Related Related

  1. 1

    Enable Macros after message box closes

  2. 2

    Fancy Box 2 automatically closes divs?

  3. 3

    SaveFileDialog closes automatically directly after calling showDialog()

  4. 4

    Way to automatically change fragments after delay

  5. 5

    Close dialog box after a message

  6. 6

    Ending a subroutine after a message box

  7. 7

    Dropdown field automatically closes after opening in IE11

  8. 8

    java joptionpane which automatically closes after few seconds

  9. 9

    Tkinter window closes automatically after Python program has run in PyCharm

  10. 10

    go to previous window automatically after printing dialog closes

  11. 11

    java joptionpane which automatically closes after few seconds

  12. 12

    Tkinter window closes automatically after Python program has run in PyCharm

  13. 13

    Dropdown field automatically closes after opening in IE11

  14. 14

    Dialog box closes slowly after clicking send button

  15. 15

    Application closes automatically

  16. 16

    Putty - Automatically closes on authentication

  17. 17

    NSPopover closes automatically

  18. 18

    Putty - Automatically closes on authentication

  19. 19

    Displaying a message box after a certain period of time

  20. 20

    Show a message box after usercontrol fully displayed

  21. 21

    Show Message Box After Button Click

  22. 22

    Value in cell is changing after a message box

  23. 23

    How to automatically Click a Button in Android after a 5 second delay

  24. 24

    show input dialog box after error message dialog box

  25. 25

    Spring Tool Suite Closes Automatically after adding dependency to pom.xml file

  26. 26

    After Showing Alert box subview button is automatically hidden in swift

  27. 27

    Widget opens for a second then automatically closes?

  28. 28

    FCM push notification closes automatically

  29. 29

    custom jquery alert closes automatically

HotTag

Archive