Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Windows.MessageBox doesn't wait for user input before going poof!

Tags:

c#

wpf

messagebox

...and it makes no sense why. T-T

In my Application_Startup event handler I have code that looks kinda like this:

private void Application_Startup(object sender, StartupEventArgs e)
{
    string errorMessage;

    if(CheckStartUpConditions(out errorMessage))
    {
        (new MainWindow()).Show();
    }
    else
    {
        MessageBox.Show(errorMessage, "Application Startup", 
            MessageBoxButton.OK, MessageBoxImage.Error);

        Shutdown();
    }
}

private bool CheckStartUpConditions(out string errorMessage)
{
    errorMessage = string.Empty;  

    if(...)
        errorMessage += "Please login to xxx. ";

    if(...)
        errorMessage += "Please install xxx.";

    if(string.IsNullOrEmpty(errorMessage))
        return true;
    else
        return false;
}

The message box makes an brief appearance for like a second before going "POOF!" It doesn't wait for me to click "OK" or on the "X" button. I'm really stumped as to why this is occuring, so any help would be greatly appreciated.

I've tried commenting out the call to Shutdown just for kicks and giggles, and it still behaves the same way.

Also, the application also has a SplashScreen, so I don't know if that's effecting this.

EDIT: I added more code if that helps. The message box is showing the correct error message. Just won't stay long enough for the users to read it. >:(

EDIT PART 2: Okay...I think I've found the culprit. :( I changed the build action on the image I'm using as my splash from SplashScreen to None and the message box will now stay and wait for user input. I don't understand why the SplashScreen is screwing with the MessageBox. >:(

like image 761
Ashley Grenon Avatar asked May 07 '11 18:05

Ashley Grenon


People also ask

What is MessageBox class explain MessageBox () in detail?

A message box is a prefabricated modal dialog box that displays a text message to a user. You show a message box by calling the static Show method of the MessageBox class. The text message that is displayed is the string argument that you pass to Show.

Which is the default MessageBox button?

The first button on the message box is the default button.

Is MessageBox show a modal dialog window?

Definition. Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user.


6 Answers

The message box vanishes immediately because it has no owner. If you specify the option MessageBoxOptions.DefaultDesktopOnly, the desktop will be assigned as the owner, and the message box will work correctly on an application with no main window.

MessageBox.Show(
    "Message", 
    "Title",
    MessageBoxButton.YesNoCancel, 
    MessageBoxImage.Question, 
    MessageBoxResult.Cancel,
    MessageBoxOptions.DefaultDesktopOnly);
like image 133
Russell Phillips Avatar answered Oct 01 '22 02:10

Russell Phillips


Based on Alexey Ivanov's suggestion, I successfully used a new window as the parent

System.Windows.Forms.MessageBox.Show(new System.Windows.Forms.NativeWindow(), errorMessage, "Application Startup", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
like image 44
Neil Avatar answered Oct 01 '22 03:10

Neil


Try to use an overload that accepts System.Windows.Window parameter and pass Null value to make your MessageBox a top-level window of your application, which is independent of all other windows that may exist. I guess your MessageBox gets owned by splashscreen form. When splashscreen is closed, the framework closes the MessageBox. So making your MessageBox ownerless should do the trick.

like image 30
Alexey Ivanov Avatar answered Oct 01 '22 03:10

Alexey Ivanov


Create transparent hidden window and use it as an owner of the MessageBox:

private Window CreateHiddenWindow()
        {
            var window = new Window
            {
                AllowsTransparency = true,
                Background = System.Windows.Media.Brushes.Transparent,
                WindowStyle = WindowStyle.None,
                Top = 0,
                Left = 0,
                Width = 1,
                Height = 1,
                ShowInTaskbar = false
            };

            window.Show();

            return window;
        }
like image 31
andrey.tsykunov Avatar answered Oct 01 '22 04:10

andrey.tsykunov


You can keep your splash screen and use a standard WPF MessageBox if you implement the splash screen in code instead of as an image build action.

App.xaml.cs:

var splashScreen = new SplashScreen("path/to/splash_image.bmp");
splashScreen.Show(false); //make sure to use 'false' here to prevent auto-closing

string errorMessage;

if(CheckStartUpConditions(out errorMessage))
{
    (new MainWindow()).Show();
}
else
{
    //standard WPF MessageBox may now be used here
    MessageBox.Show(errorMessage, "Application Startup", 
        MessageBoxButton.OK, MessageBoxImage.Error);

    Shutdown();
}

//explicitly close the splash screen now
splashScreen.Close(TimeSpan.FromSeconds(1));
like image 38
HotN Avatar answered Oct 01 '22 03:10

HotN


Alexey is right, the splash screen closes the message box.

A simple way to avoid this is to use the native MessageBox function:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

public static void Main()
{
   ...
   MessageBox(new IntPtr(0), "Hello World!", "MyApp", 0);
like image 35
Andreas Kahler Avatar answered Oct 01 '22 02:10

Andreas Kahler