How to Send messages from worker thread to main thread 
Author Message
 How to Send messages from worker thread to main thread

I need a background thread to check status information on the database every
10 seconds and update a listcontrol with the results. This thread sleeps for
ten seconds, allowing the user to work then informs the user of status info.

Reading the MSDN I assume the approach is to get a worker thread to send a
message to be processed by the apps main thread then sleep for ten seconds.
The main thread would then do the DB access and refresh the listcontrol.

Is this the way it should be done ?

How do I send such a message that can be detected by the main thread and
automatically run the required function.

I have tried
    SendNotifyMessage(*pWnd, ID_CHECK, 0, 0);
and
    PostMessage(*pWnd, ID_CHECK, 0, 0);

using message map
    ON_COMMAND(ID_CHECK, OnCheckStatus)

and also tried

BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
 NMHDR *pNotifyMessageHeader = (NMHDR *)lParam;
 if(pNotifyMessageHeader->code==ID_CHECK)
 {
  OnCheckStatus();
 }
 return CDialog::OnNotify(wParam, lParam, pResult);

Quote:
}

without success.

Any help will be much appreciatied.
---
Regards,
Paul



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread
This way I do such stuff:
1. I give the worker thread the handle of the window which is to receive the
message.
    HWND hwnd = ...;
2. I define the message
    #define WM_MYMESSAGE WM_APP+...;
3. I post the message from the worker thread
    ::PostMessage(hwnd, WM_MYMESSAGE, ..., ...);
4. I add a message handler in the class which receives the message.
    ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
    LRESULT CMyClass::OnMyMessage(WPARAM wParam, LPARAM lParam)

Not that you should use The ON_MESSAGE macro, not ON_COMMAND
and that you should give your message handler the correct prototype
(LRESULT ...).

Usually I keep the declaration of the message handler outside the AFX... block.

Hope this helps.



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread
This works a treat.

Thanks
--
Regards,
Paul


Quote:
> This way I do such stuff:
> 1. I give the worker thread the handle of the window which is to receive
the
> message.
>     HWND hwnd = ...;
> 2. I define the message
>     #define WM_MYMESSAGE WM_APP+...;
> 3. I post the message from the worker thread
>     ::PostMessage(hwnd, WM_MYMESSAGE, ..., ...);
> 4. I add a message handler in the class which receives the message.
>     ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
>     LRESULT CMyClass::OnMyMessage(WPARAM wParam, LPARAM lParam)

> Not that you should use The ON_MESSAGE macro, not ON_COMMAND
> and that you should give your message handler the correct prototype
> (LRESULT ...).

> Usually I keep the declaration of the message handler outside the AFX...
block.

> Hope this helps.



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread
You can use
::SendMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

Write a class containing all usefull information to be given to the main
thread, plus the m_hWnd of the main thread's window.

Before you create the Thread, instantiate such an object.

When you CreateThread, give as the LPARAM the address of this info
object.

In your thread, you can call SendMessage to the main thread's window.
The main thread then is able to retrieve information from the child
thread, via the info object.

Gabriel.

Quote:

> I need a background thread to check status information on the database every
> 10 seconds and update a listcontrol with the results. This thread sleeps for
> ten seconds, allowing the user to work then informs the user of status info.

> Reading the MSDN I assume the approach is to get a worker thread to send a
> message to be processed by the apps main thread then sleep for ten seconds.
> The main thread would then do the DB access and refresh the listcontrol.

> Is this the way it should be done ?

> How do I send such a message that can be detected by the main thread and
> automatically run the required function.

> I have tried
>     SendNotifyMessage(*pWnd, ID_CHECK, 0, 0);
> and
>     PostMessage(*pWnd, ID_CHECK, 0, 0);

> using message map
>     ON_COMMAND(ID_CHECK, OnCheckStatus)

> and also tried

> BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
> {
>  NMHDR *pNotifyMessageHeader = (NMHDR *)lParam;
>  if(pNotifyMessageHeader->code==ID_CHECK)
>  {
>   OnCheckStatus();
>  }
>  return CDialog::OnNotify(wParam, lParam, pResult);
> }

> without success.

> Any help will be much appreciatied.
> ---
> Regards,
> Paul



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread
This is what I do as well except for number 2 I hit View / Resource includes
and define the message there. But thats only for neatness it probably doesnt
make a difference where you do it.

Quote:

>This way I do such stuff:
>1. I give the worker thread the handle of the window which is to receive
the
>message.
>    HWND hwnd = ...;
>2. I define the message
>    #define WM_MYMESSAGE WM_APP+...;
>3. I post the message from the worker thread
>    ::PostMessage(hwnd, WM_MYMESSAGE, ..., ...);
>4. I add a message handler in the class which receives the message.
>    ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
>    LRESULT CMyClass::OnMyMessage(WPARAM wParam, LPARAM lParam)

>Not that you should use The ON_MESSAGE macro, not ON_COMMAND
>and that you should give your message handler the correct prototype
>(LRESULT ...).

>Usually I keep the declaration of the message handler outside the AFX...
block.

>Hope this helps.



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread
Have you tried PostThreadMessage()?

Quote:

>I need a background thread to check status information on the
database every
>10 seconds and update a listcontrol with the results. This thread
sleeps for
>ten seconds, allowing the user to work then informs the user of
status info.

>Reading the MSDN I assume the approach is to get a worker thread to
send a
>message to be processed by the apps main thread then sleep for ten
seconds.
>The main thread would then do the DB access and refresh the
listcontrol.

>Is this the way it should be done ?

>How do I send such a message that can be detected by the main thread
and
>automatically run the required function.

>I have tried
>    SendNotifyMessage(*pWnd, ID_CHECK, 0, 0);
>and
>    PostMessage(*pWnd, ID_CHECK, 0, 0);

>using message map
>    ON_COMMAND(ID_CHECK, OnCheckStatus)

>and also tried

>BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
>{
> NMHDR *pNotifyMessageHeader = (NMHDR *)lParam;
> if(pNotifyMessageHeader->code==ID_CHECK)
> {
>  OnCheckStatus();
> }
> return CDialog::OnNotify(wParam, lParam, pResult);
>}

>without success.

>Any help will be much appreciatied.
>---
>Regards,
>Paul



Fri, 01 Feb 2002 03:00:00 GMT  
 How to Send messages from worker thread to main thread


Quote:
>You can use
>::SendMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

>Write a class containing all usefull information to be given to the main
>thread, plus the m_hWnd of the main thread's window.

>Before you create the Thread, instantiate such an object.

>When you CreateThread, give as the LPARAM the address of this info
>object.

>In your thread, you can call SendMessage to the main thread's window.
>The main thread then is able to retrieve information from the child
>thread, via the info object.

>Gabriel.


>> I need a background thread to check status information on the database every
>> 10 seconds and update a listcontrol with the results. This thread sleeps for
>> ten seconds, allowing the user to work then informs the user of status info.

>> Reading the MSDN I assume the approach is to get a worker thread to send a
>> message to be processed by the apps main thread then sleep for ten seconds.
>> The main thread would then do the DB access and refresh the listcontrol.

>> Is this the way it should be done ?

>> How do I send such a message that can be detected by the main thread and
>> automatically run the required function.

>> I have tried
>>     SendNotifyMessage(*pWnd, ID_CHECK, 0, 0);
>> and
>>     PostMessage(*pWnd, ID_CHECK, 0, 0);

>> using message map
>>     ON_COMMAND(ID_CHECK, OnCheckStatus)

>> and also tried

>> BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
>> {
>>  NMHDR *pNotifyMessageHeader = (NMHDR *)lParam;
>>  if(pNotifyMessageHeader->code==ID_CHECK)
>>  {
>>   OnCheckStatus();
>>  }
>>  return CDialog::OnNotify(wParam, lParam, pResult);
>> }

>> without success.

>> Any help will be much appreciatied.
>> ---
>> Regards,
>> Paul

WARNING WARNING WARNING
If you are using MFC the you must use afxbeginthread and afxendthread
in order to get the RTL to intialize and quit Properly.
Unless of course you Don't use the RTL, or you like crashes, or you
want a nice leak to try and find.


Fri, 01 Feb 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Posting a message from a worker thread to the main app thread

2. worker thread vs main thread?

3. Handling events from a worker thread in the main thread

4. component in main threads used in worker threads

5. Synchronization problem between main thread and worker thread

6. Posting messages to the main window from worker threads

7. Worker Threads and Message Sending

8. How to send a message to a windowless ActiveX control from its worker thread

9. Sending messages from a worker thread

10. main GUI thread -vs- Main Frame Thread

11. COM Threading Model for ISAPI Worker Threads

12. Exe server threading model - events from worker thread question

 

 
Powered by phpBB® Forum Software