Search code examples
wpfasync-awaittaskmessagebox

WPF - Open Message Box while another Message Box is open


Hi I am testing some stuff for my program, everytime a test is complete a new Message Box should show. The problem is that only a new Message Box will open after I closed the Message Box before that. I tried putting the Message Box into a new Task, but that did not work either...

Task.Run(() => System.Windows.MessageBox.Show(
                dirs[directory] + " is done, it took " + stopwatch.Elapsed,
                "Done with " + dirs[directory],
                System.Windows.MessageBoxButton.OK,
                System.Windows.MessageBoxImage.Information,
                System.Windows.MessageBoxResult.OK,
                System.Windows.MessageBoxOptions.ServiceNotification));

I also tried to make the method async and await Task.Run, no luck there too

 await Task.Run(() => System.Windows.MessageBox.Show(
                dirs[directory] + " is done, it took " + stopwatch.Elapsed,
                "Done with " + dirs[directory],
                System.Windows.MessageBoxButton.OK,
                System.Windows.MessageBoxImage.Information,
                System.Windows.MessageBoxResult.OK,
                System.Windows.MessageBoxOptions.ServiceNotification));

Solution

  • MessageBox.Show invokes MessageBox function (winuser.h).

    On the remarks you can read:

    If you create a message box while a dialog box is present, use a handle to the dialog box as the hWnd parameter. The hWnd parameter should not identify a child window, such as a control in a dialog box.

    But you can't get the hWnd of a message box.

    You're better of creating a custom window for this.