Preventing multiple MessageBoxes

This is a discussion on Preventing multiple MessageBoxes within the Windows Programming forums, part of the Platform Specific Boards category; In a program I'm writting to practice while learning Windows, I often find myself having to inform errors to the ...

  1. #1
    Loom Weaver the dead tree's Avatar
    Join Date
    Apr 2004
    Posts
    140

    Arrow Preventing multiple MessageBoxes

    In a program I'm writting to practice while learning Windows, I often find myself having to inform errors to the user.
    Here is a quick example:
    Code:
     int * starLoc = new int[133];
    	if (  starLoc == NULL )
    	{ // if no memory...
    		MessageBox(NULL,TEXT("Not enough memory to run this application."),
    		TEXT("Error."),MB_OK | MB_ICONWARNING );
    		PostQuitMessage(0);
    	}
    Here an array of intergers is created during a WM_PAINT, and if memory is not enough a MessageBox is displayed, and so it will be again and again if any client area region becomes invalid.
    I could send a variable to other functions to test conditions and see wheter a new MessageBox should pop or not, but I was wondering if there is any other way to deal with this.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. One is to use a boolean variable to indicate whether a message window is already visible.

    Kuphryn

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    What you have to realize is that MessageBox() contains a message-loop, which will send more WM_PAINT's to your window even though the last call to your windows procedure is still blocked in the call to MessageBox().

    What kuphryn is suggesting is that you use a static (or global) boolean variable to indicate that you're waiting on a result from a call to MessageBox() to prevent displaying anonther one.

    gg

  4. #4
    Loom Weaver the dead tree's Avatar
    Join Date
    Apr 2004
    Posts
    140
    Using a static variable is what I had in mind, but since I am completely new at programming I was just wondering if there was a common better way that people used.
    Thanks

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    I would guess that the more "common" thing to do is not have calls to MessageBox() in your WM_PAINT handler.

    Unless your debugging or verifying something, in which case you can use a static or global to prevent recursion

    gg

  6. #6
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    I tend to use OutputDebugString() - when in debug mode it will print any text to the debug window. But if you need to inform the user in the release build, then definitely a boolean variable to stop the messagebox called more than once, or as has been already stated, don't use it in a WM_PAINT message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21