Prev: CRepeatButton
Next: Knit picking
From: Jason .Y on 14 Sep 2009 04:25 Hi,I have come to meet a strange question in my application: I have 2 Thread, which are both created by AfxBeginThread function with a parameter of a derived class of CWinThread: MsgCenterThread MainTestThread and the MainTestThread is always trying to post thread messages to MsgCenterThread, The messages are defined as below: #define APPMSG_SHOWDLG (WM_USER + 211) #define APPMSG_DATA (WM_USER + 212) The APPMSG_DATA is send 1 time per second, and the lParam is the count of sending. In case of APPMSG_SHOWDLG, the MsgCenterThread will create a CDialog object and call the DoModal method.and in another case, The MsgCenterThread will try to printf the sending count, which is attached with the APPMSG_DATA as lParam . The application seems to work fine,but once the MsgCenter has received the APPMSG_SHOWDLG and show the Dialog, the MsgCenterThread can't receive the APPMSG_DATA message for sometimes when I just clicked on the Dialog very frequently. By the way, My coding environment is :Windows XP + VS2005 Any suggestion will be appriciated. Jason
From: Doug Harrison [MVP] on 14 Sep 2009 09:15 On Mon, 14 Sep 2009 01:25:51 -0700 (PDT), "Jason .Y" <lin.yang.jason(a)gmail.com> wrote: >Hi,I have come to meet a strange question in my application: > >I have 2 Thread, which are both created by AfxBeginThread function >with a parameter of a derived class of CWinThread: >MsgCenterThread >MainTestThread > >and the MainTestThread is always trying to post thread messages to >MsgCenterThread, The messages are defined as below: >#define APPMSG_SHOWDLG (WM_USER + 211) >#define APPMSG_DATA (WM_USER + 212) > >The APPMSG_DATA is send 1 time per second, and the lParam is the count >of sending. > >In case of APPMSG_SHOWDLG, the MsgCenterThread will create a CDialog >object and call the DoModal method.and in another case, The >MsgCenterThread will try to printf the sending count, which is >attached with the APPMSG_DATA as lParam . > >The application seems to work fine,but once the MsgCenter has received >the APPMSG_SHOWDLG and show the Dialog, the MsgCenterThread can't >receive the APPMSG_DATA message for sometimes when I just clicked on >the Dialog very frequently. > >By the way, My coding environment is :Windows XP + VS2005 > >Any suggestion will be appriciated. PRB: PostThreadMessage Messages Lost When Posted to UI Thread http://support.microsoft.com/kb/183116 Also, if you're going to use thread messages, you can start numbering them anywhere. Basing them on WM_USER is kinda strange :) For more on what the message ranges mean, see: Which message numbers belong to whom? http://blogs.msdn.com/oldnewthing/archive/2003/12/02/55914.aspx That said, it's a good idea to keep the UI all in one thread, your program's main thread. Presumably, it has some sort of window that could be the target for all your custom WM_APP-based messages. If not, you can create a hidden message-only window for this purpose. -- Doug Harrison Visual C++ MVP
From: Jason .Y on 14 Sep 2009 23:16 On 9ÔÂ14ÈÕ, ÏÂÎç9ʱ15·Ö, "Doug Harrison [MVP]" <d....(a)mvps.org> wrote: > PRB: PostThreadMessage Messages Lost When Posted to UI Threadhttp://support.microsoft.com/kb/183116 > > Also, if you're going to use thread messages, you can start numbering them > anywhere. Basing them on WM_USER is kinda strange :) For more on what the > message ranges mean, see: > > Which message numbers belong to whom?http://blogs.msdn.com/oldnewthing/archive/2003/12/02/55914.aspx > > That said, it's a good idea to keep the UI all in one thread, your > program's main thread. Presumably, it has some sort of window that could be > the target for all your custom WM_APP-based messages. If not, you can > create a hidden message-only window for this purpose. > > -- > Doug Harrison > Visual C++ MVP Thank you very much for your help! but I still have a question:according to the article <PRB: PostThreadMessage Messages Lost When Posted to UI Thread>, all the message send to a UI thread by PostThreadMessage would be lost when the UI Thread is showing a modal dialog box. "When an UI thread is involved in modal behavior, the thread pumps messages in a message loop internal to the modal system rather than in the thread's main message loop. Messages that are posted to a window can still be dispatched to the window procedure of the target window, because the messages are associated with a window. However, thread messages need to be handled directly by the message loop, because they cannot be automatically dispatched elsewhere. Since the secondary message loop does not know about the thread message, it will be dropped." but in fact,the message won't be lost if I don't manipulate the dialog very frequently. then I'm comfused:why message send by PostThreadMessage would not get lost in this situation?As the message loop internal to the modal dialog is the primary message loop.the thread message should be drop. ---- Jason
From: Doug Harrison [MVP] on 15 Sep 2009 01:05 On Mon, 14 Sep 2009 20:16:02 -0700 (PDT), "Jason .Y" <lin.yang.jason(a)gmail.com> wrote: >Thank you very much for your help! > >but I still have a question:according to the article <PRB: >PostThreadMessage Messages Lost When Posted to UI Thread>, all the >message send to a UI thread by PostThreadMessage would be lost when >the UI Thread is showing a modal dialog box. > >"When an UI thread is involved in modal behavior, the thread pumps >messages in a message loop internal to the modal system rather than in >the thread's main message loop. Messages that are posted to a window >can still be dispatched to the window procedure of the target window, >because the messages are associated with a window. However, thread >messages need to be handled directly by the message loop, because they >cannot be automatically dispatched elsewhere. Since the secondary >message loop does not know about the thread message, it will be >dropped." > >but in fact,the message won't be lost if I don't manipulate the dialog >very frequently. then I'm comfused:why message send by >PostThreadMessage would not get lost in this situation?As the message >loop internal to the modal dialog is the primary message loop.the >thread message should be drop. IIRC, MFC runs its own message loop for modal dialogs, and it must be handling thread messages in some way. I stopped using thread messages a long time ago, and I don't remember how MFC handles them. You can set a breakpoint on your message handler, and looking at the function call stack in the debugger will tell you how you got there. As for the messages being lost when you "manipulate" the dialog, that must be due to Windows running a secondary message loop per the KB article. -- Doug Harrison Visual C++ MVP
From: Jason .Y on 15 Sep 2009 01:39
On 9ÔÂ15ÈÕ, ÏÂÎç1ʱ05·Ö, "Doug Harrison [MVP]" <d....(a)mvps.org> wrote: > On Mon, 14 Sep 2009 20:16:02 -0700 (PDT), "Jason .Y" > IIRC, MFC runs its own message loop for modal dialogs, and it must be > handling thread messages in some way. I stopped using thread messages a > long time ago, and I don't remember how MFC handles them. You can set a > breakpoint on your message handler, and looking at the function call stack > in the debugger will tell you how you got there. As for the messages being > lost when you "manipulate" the dialog, that must be due to Windows running > a secondary message loop per the KB article. > > -- > Doug Harrison > Visual C++ MVP Thank you. |