-
December 7th, 2010, 09:31 AM
#1
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?
-
December 7th, 2010, 10:58 AM
#2
Re: Weird bug with MessageBox function
I think you'll need to show us the code in question
JeffB
-
December 7th, 2010, 11:45 AM
#3
Re: Weird bug with MessageBox function
Originally Posted by JeffB
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);
}
-
December 7th, 2010, 01:06 PM
#4
Re: Weird bug with MessageBox function
Originally Posted by weggo
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
-
December 7th, 2010, 01:29 PM
#5
Re: Weird bug with MessageBox function
Originally Posted by Paul McKenzie
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|