Best notification method. 
Author Message
 Best notification method.

Hi

I have an application which when starts up, spwans a thread. THe application
will then proceed to create n modelss windows from time to time based on the
user inputs.  Now some part of the UI for these windows has to be changed
based on the proceedings of the spawed thread. Also all the windows are run
by the ONE message loop.

I dont want to access the UI elements of the created windows directly from
the spawned thread. Also the message handlers of the created windows use
global data and is not safe to be called from the thread.

I know that if i *POST* a message from the thread to the n windows
seperately, this could be solved and thread safety will be assured.

Is there a better way to do it other than posting?  Basically i want the
notification from the spawned thread to execute in the context of the main
thread.

Thankx

Rajesh



Mon, 27 Oct 2003 01:05:47 GMT  
 Best notification method.
Rajesh,

Can't you *POST* (or *SEND*) a single message from the spawned thread
to the main thread, and directly access methods and member data of all your
modeless windows (as well as global data, of course)  from the main thread
in response to that  message?

Rufus


Quote:
> Hi

> I have an application which when starts up, spwans a thread. THe
application
> will then proceed to create n modelss windows from time to time based on
the
> user inputs.  Now some part of the UI for these windows has to be changed
> based on the proceedings of the spawed thread. Also all the windows are
run
> by the ONE message loop.

> I dont want to access the UI elements of the created windows directly from
> the spawned thread. Also the message handlers of the created windows use
> global data and is not safe to be called from the thread.

> I know that if i *POST* a message from the thread to the n windows
> seperately, this could be solved and thread safety will be assured.

> Is there a better way to do it other than posting?  Basically i want the
> notification from the spawned thread to execute in the context of the main
> thread.

> Thankx

> Rajesh



Sun, 26 Oct 2003 22:00:13 GMT  
 Best notification method.
Yes,.. But one thing..
The application may have some windows or may not.. If the application doesnt
have anywindows, how can i post or send mesage to the main thread?

-Rajesh



Quote:
> Rajesh,

> Can't you *POST* (or *SEND*) a single message from the spawned thread
> to the main thread, and directly access methods and member data of all
your
> modeless windows (as well as global data, of course)  from the main thread
> in response to that  message?

> Rufus



> > Hi

> > I have an application which when starts up, spwans a thread. THe
> application
> > will then proceed to create n modelss windows from time to time based on
> the
> > user inputs.  Now some part of the UI for these windows has to be
changed
> > based on the proceedings of the spawed thread. Also all the windows are
> run
> > by the ONE message loop.

> > I dont want to access the UI elements of the created windows directly
from
> > the spawned thread. Also the message handlers of the created windows use
> > global data and is not safe to be called from the thread.

> > I know that if i *POST* a message from the thread to the n windows
> > seperately, this could be solved and thread safety will be assured.

> > Is there a better way to do it other than posting?  Basically i want the
> > notification from the spawned thread to execute in the context of the
main
> > thread.

> > Thankx

> > Rajesh



Mon, 27 Oct 2003 01:29:32 GMT  
 Best notification method.
PostThreadMessage() ?

Ian.

Quote:

> Yes,.. But one thing..
> The application may have some windows or may not.. If the application
doesnt
> have anywindows, how can i post or send mesage to the main thread?

> -Rajesh



> > Rajesh,

> > Can't you *POST* (or *SEND*) a single message from the spawned thread
> > to the main thread, and directly access methods and member data of all
> your
> > modeless windows (as well as global data, of course)  from the main
thread
> > in response to that  message?

> > Rufus



> > > Hi

> > > I have an application which when starts up, spwans a thread. THe
> > application
> > > will then proceed to create n modelss windows from time to time based
on
> > the
> > > user inputs.  Now some part of the UI for these windows has to be
> changed
> > > based on the proceedings of the spawed thread. Also all the windows
are
> > run
> > > by the ONE message loop.

> > > I dont want to access the UI elements of the created windows directly
> from
> > > the spawned thread. Also the message handlers of the created windows
use
> > > global data and is not safe to be called from the thread.

> > > I know that if i *POST* a message from the thread to the n windows
> > > seperately, this could be solved and thread safety will be assured.

> > > Is there a better way to do it other than posting?  Basically i want
the
> > > notification from the spawned thread to execute in the context of the
> main
> > > thread.

> > > Thankx

> > > Rajesh



Sun, 26 Oct 2003 23:58:28 GMT  
 Best notification method.

Quote:

> Yes,.. But one thing..
> The application may have some windows or may not.. If the application doesnt
> have anywindows, how can i post or send mesage to the main thread?

> -Rajesh

As Ian said, you could use PostThreadMessage, but be warned that it is
dangerous to use PostThreadMessage when the main thread does have
windows.  The safest approach would be to use PostMessage to a main
window when it exists and switch to PostThreadMessage only when no
windows exist.

--
Scott McPhillips [VC++ MVP]



Mon, 27 Oct 2003 09:22:40 GMT  
 Best notification method.
When i use PostThreadMessage,  I use *BOTH* ON_THREAD_MESSAGE and the
PreTranslateMessage to map to the message handler.. Still i lose messages..
Any idea why?

-Rajesh


Quote:

> > Yes,.. But one thing..
> > The application may have some windows or may not.. If the application
doesnt
> > have anywindows, how can i post or send mesage to the main thread?

> > -Rajesh

> As Ian said, you could use PostThreadMessage, but be warned that it is
> dangerous to use PostThreadMessage when the main thread does have
> windows.  The safest approach would be to use PostMessage to a main
> window when it exists and switch to PostThreadMessage only when no
> windows exist.

> --
> Scott McPhillips [VC++ MVP]



Mon, 27 Oct 2003 13:15:58 GMT  
 Best notification method.

Quote:

> When i use PostThreadMessage,  I use *BOTH* ON_THREAD_MESSAGE and the
> PreTranslateMessage to map to the message handler.. Still i lose messages..
> Any idea why?

> -Rajesh

Yes, it is a known limitation.  Do not use PostThreadMessage to a GUI
thread.
See this Knowledge Base article:
PRB: PostThreadMessage Messages Lost When Posted to UI Thread
Q183116

--
Scott McPhillips [VC++ MVP]



Mon, 27 Oct 2003 10:48:06 GMT  
 Best notification method.
Quote:

> When i use PostThreadMessage,  I use *BOTH* ON_THREAD_MESSAGE and the
> PreTranslateMessage to map to the message handler.. Still i lose

messages..

It's because of code similar to this:

   while ( GetMessage(...) )
   {
        TranslateMessage();
        DispatchMessage();
  }

which you find handling modal dialog boxes, menus, etc.

When you post a message to a thread, the hwnd member of the message
structure is necessarily NULL. DispatchMessage() only knows how to dispatch
messages to window procedures. Consequently, successfully posted messages
end up on the floor.

You can take the advice of the KB article on not use the function, or use it
but protect yourself by putting up a thread specific message filter hook so
that you get first crack at the messages. Check the docs for
SetWindowsHookEx(WH_MSGFILTER, ...) and MessageProc().

Regards,
Will



Mon, 27 Oct 2003 12:30:08 GMT  
 Best notification method.
I have been using PostThreadMessage() with windows. There are no problems at
all. Infact CWinApp class PostThreadMessage() function.
We can write handler for the message in CWinApp class and redirect to view
class or mainframe class.

We should not use SendMessage() from another thread. That is only
limitation.




Quote:
> > When i use PostThreadMessage,  I use *BOTH* ON_THREAD_MESSAGE and the
> > PreTranslateMessage to map to the message handler.. Still i lose
> messages..

> It's because of code similar to this:

>    while ( GetMessage(...) )
>    {
>         TranslateMessage();
>         DispatchMessage();
>   }

> which you find handling modal dialog boxes, menus, etc.

> When you post a message to a thread, the hwnd member of the message
> structure is necessarily NULL. DispatchMessage() only knows how to
dispatch
> messages to window procedures. Consequently, successfully posted messages
> end up on the floor.

> You can take the advice of the KB article on not use the function, or use
it
> but protect yourself by putting up a thread specific message filter hook
so
> that you get first crack at the messages. Check the docs for
> SetWindowsHookEx(WH_MSGFILTER, ...) and MessageProc().

> Regards,
> Will



Tue, 28 Oct 2003 00:30:05 GMT  
 Best notification method.


Quote:
> I have been using PostThreadMessage() with windows. There are no problems
at
> all. Infact CWinApp class PostThreadMessage() function.
> We can write handler for the message in CWinApp class and redirect to view
> class or mainframe class.

If you PostThreadMessage() to a thread which is currently displaying a menu
or a modal dialog box (other special cases exist as well) then it will
appear to have been lost.

It may appear to work for you. Still, I suggest you read the KB article
Q183116
that Scott suggested.

Regards,
Will



Tue, 28 Oct 2003 03:23:56 GMT  
 Best notification method.
I read the article and I wrote a sample to test it.
Yes, you are right. Some messages sent by PostThreadMessage() are not
processed while resizing the window.



Quote:


> > I have been using PostThreadMessage() with windows. There are no
problems
> at
> > all. Infact CWinApp class PostThreadMessage() function.
> > We can write handler for the message in CWinApp class and redirect to
view
> > class or mainframe class.

> If you PostThreadMessage() to a thread which is currently displaying a
menu
> or a modal dialog box (other special cases exist as well) then it will
> appear to have been lost.

> It may appear to work for you. Still, I suggest you read the KB article
> Q183116
> that Scott suggested.

> Regards,
> Will



Wed, 29 Oct 2003 10:06:13 GMT  
 Best notification method.
Hi,

I experienced the 'message disappearance' issue also when working on an MFC
app 2 months back. And fixed it. (My messages still get through, even when
menu's or resizing is performed)

My 'worker thread' uses code, including this line:

  if (!::PostMessage(m_AppWindowHandle, MsgID, wParam, lParam))

to send messages to the user interface thread, which is reponsible for
driving all dialogs, windows, menu's, etc.
The UI thread uses lines like this:

   if (m_CommThread->PostThreadMessage(WM_COMM_BOOT, num, (DWORD)it_msg))

to send messages to my worker thread.

All custom messages sent back and forth are WM_USER+<offset> (WM_USER_HACK
is a #define which holds the lowest custom message number WM_USER+<offset>)
and some appeared to get lost once you started browsing lazily through the
menus or during modal dialog entry. This is fixed by adding decode/dispatch
code in the user interface thread at these spots:

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
 // detect those custom WM_USER+XXX messages we use [...]
 // but only after others have had a peek at them.
 BOOL ret;

 ret = CFrameWnd::PreTranslateMessage(pMsg);
 if (!ret && pMsg->message > WM_USER_HACK)
 {
...

Quote:
}

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 // It shows that PostMessage(WM_USER+XXX) ends up here while a user is
messing around inside
 // a menu or other modal operation. So we need to reroute the messages
arriving here too!
 if (message > WM_USER_HACK)
 {
...

Quote:
}

BOOL CCloneView::PreTranslateMessage(MSG* pMsg)
{
 // detect those custom WM_USER+XXX messages we use [...]
 // but only after others have had a peek at them.
 BOOL ret;

 ret = CFormView::PreTranslateMessage(pMsg);
 if (!ret && pMsg->message > WM_USER_HACK)
 {
...

Quote:
}

Note that the posted messages do not pass through the various
PreTranslateMessage() functions you can get at while the menu's and other
regular scores are handled by MFC. Your messages, however, *DO* turn up in
CMainFrame:WindowProc() then.

I admit that my solution to the message routing problem might be considered
'bad form' as it is a clear case of 'quick & dirty MFC hacking' but it
worked for me.

One hint though: throw in couple of TRACE() calls in these functions when
you are testing your stuff and run your app from the de{*filter*} or run the
DBWIN32 app in parallel to have a peek at those TRACE messages to see where
your messages turn up at run-time.

I did not know about the KB article at the time. Hope this stuff
nevertheless is useful to you.

--
Groetjes/Greetinx,

  Ger

----------------------------------------------------------------------------
--
Ger Hobbelt a.k.a. Insh_Allah   (remove all 'f' and 'g')

----------------------------------------------------------------------------
--
  You're a modern day Man,
  You Chat, Email and ICQ whenever you can.
  Me, I'm rather square,
  I like to Talk to people anywhere.
  'cause looking at her Eyes,
  beats querty by far at 'Nice'.



Quote:
> I read the article and I wrote a sample to test it.
> Yes, you are right. Some messages sent by PostThreadMessage() are not
> processed while resizing the window.





> > > I have been using PostThreadMessage() with windows. There are no
> problems
> > at
> > > all. Infact CWinApp class PostThreadMessage() function.
> > > We can write handler for the message in CWinApp class and redirect to
> view
> > > class or mainframe class.

> > If you PostThreadMessage() to a thread which is currently displaying a
> menu
> > or a modal dialog box (other special cases exist as well) then it will
> > appear to have been lost.

> > It may appear to work for you. Still, I suggest you read the KB article
> > Q183116
> > that Scott suggested.

> > Regards,
> > Will



Thu, 30 Oct 2003 08:35:22 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Clipboard notification DAdvise method

2. No notification received by OnConnect Method

3. Function pointers as an alternative to connection point for event notification - is it a good choice

4. ... in macros (or a better method)

5. Any good methods for generating pseudo random integer?

6. any good methods for generating psuedo random numbers

7. Best method of updating a file

8. Best method of creating a direcroty selector

9. Better pointer reassignment method ??

10. Best method for exception handling in C?

11. New MoneyMaking Method - NO MLM Involved - 5 times better then MLM

12. extern structure variable: best method?

 

 
Powered by phpBB® Forum Software