Weird bug with MessageBox function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Weird bug with MessageBox function

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Question Weird bug with MessageBox function

    So I've been using the windows MessageBox api to test my code for bugs etc. Today, when I coded sumthing and tested it with some Messagebox functions, the code didn't act like I thought it would. The problem was, that when I called function from another source with messagebox api there, the code returned or something and the code which was under the function call didnt got executed. When i commented those messagebox functions in the source file, the code ran well without any problems. So i wanted to ask, is there something i dont know about messagebox function then, or why it acts like that?

  2. #2
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Weird bug with MessageBox function

    I think you'll need to show us the code in question

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  3. #3
    Join Date
    Dec 2010
    Posts
    3

    Re: Weird bug with MessageBox function

    Quote Originally Posted by JeffB View Post
    I think you'll need to show us the code in question

    JeffB
    okay dunno if this could be in code tags(didnt work for me lol)

    CODE:

    //timer i use in my project, this is the working version

    runonce = true;


    VOID CALLBACK timer(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
    {
    if(runonce)
    {
    runonce = false;
    //messagebox api in this function, the function is located
    //in another source file(extern int declaration in header)
    run(RUNCODE_ONCE); //this function causes the return
    }
    run(RUNCODE_NULL);
    }

    //now, the not-working version
    VOID CALLBACK timer(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
    {
    if(runonce)
    {
    run(RUNCODE_ONCE); //this function causes the return,
    //so the runonce value wont change
    runonce = false;
    }
    run(RUNCODE_NULL);
    }

    //also, this version doesn't work either
    VOID CALLBACK timer(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
    {
    if(runonce)
    {
    MessageBox(NULL, "test", "test", NULL); //causes break in if statement
    runonce = false;
    }
    run(RUNCODE_NULL);
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Weird bug with MessageBox function

    Quote Originally Posted by weggo View Post
    So I've been using the windows MessageBox api to test my code for bugs etc.
    Don't use MessageBox() for this purpose. Instead, use OutputDebugString() to output diagnostic messages.

    The reason why MessageBox() shouldn't be used, and probably the reason why you are experiencing problems is that you are invoking a modal dialog in the middle of your Windows app, interrupting any processing (the main message pump being one) that may need to happen in a timely fashion. If you're using timers, you are definitely messing up the processing of such timer by introducing a modal dialog in the middle of all of that processing.

    MessageBox() and invoking modal dialogs should be used in well-defined and well-determined parts of your app, for example, a file is missing, so you inform the user, or the user chooses a menu item and you display a dialog for them to fill out, etc.

    Invoking modal dialogs just any place in a Windows app is not guaranteed to work, and is totally unnecessary for debugging when you have the OutputDebugString() API for this purpose.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2010
    Posts
    3

    Re: Weird bug with MessageBox function

    Quote Originally Posted by Paul McKenzie View Post
    Don't use MessageBox() for this purpose. Instead, use OutputDebugString() to output diagnostic messages.

    The reason why MessageBox() shouldn't be used, and probably the reason why you are experiencing problems is that you are invoking a modal dialog in the middle of your Windows app, interrupting any processing (the main message pump being one) that may need to happen in a timely fashion. If you're using timers, you are definitely messing up the processing of such timer by introducing a modal dialog in the middle of all of that processing.

    MessageBox() and invoking modal dialogs should be used in well-defined and well-determined parts of your app, for example, a file is missing, so you inform the user, or the user chooses a menu item and you display a dialog for them to fill out, etc.

    Invoking modal dialogs just any place in a Windows app is not guaranteed to work, and is totally unnecessary for debugging when you have the OutputDebugString() API for this purpose.

    Regards,

    Paul McKenzie
    Okay thank you for this information, didn't actually know about that function. Gonna use that one hereafter

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center