Multithreading - passing message to a window question
-
Hi I have an app that use multithreading to update the display of a window. I use AfxBeginThread() to spawn the thread, passing a copy of the window's m_hWnd as its parameter. This thread will then execute SendMessage() to update the window's display. This works OK in the Debug mode. However, in the release mode, it always gives error (The app has encountered a problem and will be closed etc2) whenever I have spawned two of the said threads, and then I switched window! This error still occurs even if I make the member function processing the message to do no processing (i.e. it just returns). However, this error does not occur if I comment out the SendMessage() line in the thread's function. I have tried using PostMessage() instead of SendMessage(), but with the same result. Could someone tell me what's the possible cause for this? Thanks!
-
Hi I have an app that use multithreading to update the display of a window. I use AfxBeginThread() to spawn the thread, passing a copy of the window's m_hWnd as its parameter. This thread will then execute SendMessage() to update the window's display. This works OK in the Debug mode. However, in the release mode, it always gives error (The app has encountered a problem and will be closed etc2) whenever I have spawned two of the said threads, and then I switched window! This error still occurs even if I make the member function processing the message to do no processing (i.e. it just returns). However, this error does not occur if I comment out the SendMessage() line in the thread's function. I have tried using PostMessage() instead of SendMessage(), but with the same result. Could someone tell me what's the possible cause for this? Thanks!
Since the prblm is with release build only , mostly it will be a memory initialiaztion/allocation prblm. Since both SendMessage and PostMessage failed, mostly the prblm will be with the initialiaztion of one of the parameter to these functions. chk whether hwnd is initialized as below.
HWND hWnd = NULL;
or useAfxGetApp()->GetMainWnd()->SendMessage
rgds .. mil10 -
Hi I have an app that use multithreading to update the display of a window. I use AfxBeginThread() to spawn the thread, passing a copy of the window's m_hWnd as its parameter. This thread will then execute SendMessage() to update the window's display. This works OK in the Debug mode. However, in the release mode, it always gives error (The app has encountered a problem and will be closed etc2) whenever I have spawned two of the said threads, and then I switched window! This error still occurs even if I make the member function processing the message to do no processing (i.e. it just returns). However, this error does not occur if I comment out the SendMessage() line in the thread's function. I have tried using PostMessage() instead of SendMessage(), but with the same result. Could someone tell me what's the possible cause for this? Thanks!
-
Hi I have an app that use multithreading to update the display of a window. I use AfxBeginThread() to spawn the thread, passing a copy of the window's m_hWnd as its parameter. This thread will then execute SendMessage() to update the window's display. This works OK in the Debug mode. However, in the release mode, it always gives error (The app has encountered a problem and will be closed etc2) whenever I have spawned two of the said threads, and then I switched window! This error still occurs even if I make the member function processing the message to do no processing (i.e. it just returns). However, this error does not occur if I comment out the SendMessage() line in the thread's function. I have tried using PostMessage() instead of SendMessage(), but with the same result. Could someone tell me what's the possible cause for this? Thanks!
I presume you are passing a custom message to your window, e.g. WM_MYMESSAGE or something (defined from WM_USER + or you've used RegisterMessage)? I'm also assuming that your message handler is something like: LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); You MUST make sure that you include the WPARAM and LPARAM arguments. Omitting them will work in DEBUG but will NOT work in release build. This is due to the way the stack is padded in debug builds. In a release build, the message map will expect the arguments - if they're not there, you will corrupt the stack, resulting in an exception. Apologies if this is not what you're after, I'm just making blind assumptions from your original post.
-
I presume you are passing a custom message to your window, e.g. WM_MYMESSAGE or something (defined from WM_USER + or you've used RegisterMessage)? I'm also assuming that your message handler is something like: LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); You MUST make sure that you include the WPARAM and LPARAM arguments. Omitting them will work in DEBUG but will NOT work in release build. This is due to the way the stack is padded in debug builds. In a release build, the message map will expect the arguments - if they're not there, you will corrupt the stack, resulting in an exception. Apologies if this is not what you're after, I'm just making blind assumptions from your original post.