Message passing between threads? 
Author Message
 Message passing between threads?

I have a view class that spawns a thread which downloads a file from
the internet.  I tried to use PostMessage in the spawned thread to
tell the view when the download is finished.  I get a compiler error
stating that the number of parameters in PostMessage is wrong no
matter how many parameters there are (I have tried 0-4 parameters with
the same result.  I have tried preceding PostMessage with the ::
operator in an attempt to get the WINAPI version rather than the CWnd
version since the CWnd version does not include a destination thread
among the parameters.  I am quite confused at this point.

Any help would be greatly appreciated.
Thanks



Sat, 18 Aug 2001 03:00:00 GMT  
 Message passing between threads?
jcpurvis:

I'm not sure what is going wrong for you, but the docs are pretty clear:

CWnd::PostMessage() takes 3 parameters, but the last two are optional
(default to zero).

::PostMessage() takes 4 parameters, all of which must be specified (but
the last two may not be needed).

You do not talk about the message handler, but in my experience it is
always best to declare the handler in the form

afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

HTH,

David Wilkinson

===========

Perhaps you should show us some code ...

HTH,

David Wilkinson

======================

Quote:

> I have a view class that spawns a thread which downloads a file from
> the internet.  I tried to use PostMessage in the spawned thread to
> tell the view when the download is finished.  I get a compiler error
> stating that the number of parameters in PostMessage is wrong no
> matter how many parameters there are (I have tried 0-4 parameters with
> the same result.  I have tried preceding PostMessage with the ::
> operator in an attempt to get the WINAPI version rather than the CWnd
> version since the CWnd version does not include a destination thread
> among the parameters.  I am quite confused at this point.

> Any help would be greatly appreciated.
> Thanks



Sat, 18 Aug 2001 03:00:00 GMT  
 Message passing between threads?

Quote:
>... I get a compiler error
>stating that the number of parameters in PostMessage is wrong no
>matter how many parameters there are..

Post the line of code you have tried along with the error message.
It's hard to guess what is wrong.

Dave
----
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sat, 18 Aug 2001 03:00:00 GMT  
 Message passing between threads?
jcpurvis:

You are posting the message to the main window, but your message handler
is in the view. MFC routes WM_COMMAND messages to the view, document or
application, but other messages go only to the window targeted by the
message.

You could either handle the message in the main window, or figure out a
way to get a pointer or handle to the view in the thread. I don't know
how you are using the LPVOID parameter of the thread procedure, but what
I do is use this parameter to pass a pointer to the window which is to
receive the message.

HTH,

David Wilkinson

==============

Quote:

> Sorry about not being specific enough.

> While compiling code in preparation for this message, the
> compile worked!  However, I don't seem to be getting a
> response from the message handler (no message box
> appears).  I can tell that the thread executes because it
> successfully downloads a test file.  Did I specify the
> destination window for the message correctly?  (This is
> a doc/view where the view contains the message
> handler).

> Here are code snippets to illustrate the problem:

> Thread code:

> UINT DownLoadFileThreadProc(LPVOID pParam)
> {
>         Download of file

>         ::PostMessage((HWND) (AfxGetApp()->m_pMainWnd),
> WM_DOWNLOADFILE, 0, 0);
> }

> Message Handler:

> #define WM_DOWNLOADFILE                 WM_USER+1

>         ON_MESSAGE(WM_DOWNLOADFILE, OnDownLoadFileMsg)

> afx_msg LRESULT CSiteGrabberView::OnDownLoadFileMsg(WPARAM wParam,
> LPARAM lParam)
> {
>         AfxMessageBox("OnDownLoadFileMsg here!");
>         return 0;
> }



> >>... I get a compiler error
> >>stating that the number of parameters in PostMessage is wrong no
> >>matter how many parameters there are..

> >Post the line of code you have tried along with the error message.
> >It's hard to guess what is wrong.

> >Dave
> >----
> >My address is altered to discourage junk mail.
> >Please post responses to the newsgroup thread,
> >there's no need for follow-up email copies.



Sat, 18 Aug 2001 03:00:00 GMT  
 Message passing between threads?

Quote:

> Sorry about not being specific enough.

> While compiling code in preparation for this message, the
> compile worked!  However, I don't seem to be getting a
> response from the message handler (no message box
> appears).  I can tell that the thread executes because it
> successfully downloads a test file.  Did I specify the
> destination window for the message correctly?  (This is
> a doc/view where the view contains the message
> handler).

> Here are code snippets to illustrate the problem:

> Thread code:

> UINT DownLoadFileThreadProc(LPVOID pParam)
> {
>         Download of file

>         ::PostMessage((HWND) (AfxGetApp()->m_pMainWnd),
> WM_DOWNLOADFILE, 0, 0);
> }

> Message Handler:

> #define WM_DOWNLOADFILE                 WM_USER+1

>         ON_MESSAGE(WM_DOWNLOADFILE, OnDownLoadFileMsg)

> afx_msg LRESULT CSiteGrabberView::OnDownLoadFileMsg(WPARAM wParam,
> LPARAM lParam)
> {
>         AfxMessageBox("OnDownLoadFileMsg here!");
>         return 0;
> }

The HWND of the mainframe is a member variable.  Instead of the HWND cast try:

  AfxGetApp()->m_pMainWnd->m_hWnd



Sat, 18 Aug 2001 03:00:00 GMT  
 Message passing between threads?
Sorry about not being specific enough.

While compiling code in preparation for this message, the
compile worked!  However, I don't seem to be getting a
response from the message handler (no message box
appears).  I can tell that the thread executes because it
successfully downloads a test file.  Did I specify the
destination window for the message correctly?  (This is
a doc/view where the view contains the message
handler).

Here are code snippets to illustrate the problem:

Thread code:

UINT DownLoadFileThreadProc(LPVOID pParam)
{
        Download of file

        ::PostMessage((HWND) (AfxGetApp()->m_pMainWnd),
WM_DOWNLOADFILE, 0, 0);

Quote:
}

Message Handler:

#define WM_DOWNLOADFILE                 WM_USER+1

        ON_MESSAGE(WM_DOWNLOADFILE, OnDownLoadFileMsg)

afx_msg LRESULT CSiteGrabberView::OnDownLoadFileMsg(WPARAM wParam,
LPARAM lParam)
{
        AfxMessageBox("OnDownLoadFileMsg here!");
        return 0;

Quote:
}



Quote:
>>... I get a compiler error
>>stating that the number of parameters in PostMessage is wrong no
>>matter how many parameters there are..

>Post the line of code you have tried along with the error message.
>It's hard to guess what is wrong.

>Dave
>----
>My address is altered to discourage junk mail.
>Please post responses to the newsgroup thread,
>there's no need for follow-up email copies.



Sun, 19 Aug 2001 03:00:00 GMT  
 Message passing between threads?
Thanks for the help - it worked.

Jim

On Tue, 02 Mar 1999 22:13:35 -0500, David Wilkinson

Quote:

>jcpurvis:

>You are posting the message to the main window, but your message handler
>is in the view. MFC routes WM_COMMAND messages to the view, document or
>application, but other messages go only to the window targeted by the
>message.

>You could either handle the message in the main window, or figure out a
>way to get a pointer or handle to the view in the thread. I don't know
>how you are using the LPVOID parameter of the thread procedure, but what
>I do is use this parameter to pass a pointer to the window which is to
>receive the message.

>HTH,

>David Wilkinson

>==============


>> Sorry about not being specific enough.

>> While compiling code in preparation for this message, the
>> compile worked!  However, I don't seem to be getting a
>> response from the message handler (no message box
>> appears).  I can tell that the thread executes because it
>> successfully downloads a test file.  Did I specify the
>> destination window for the message correctly?  (This is
>> a doc/view where the view contains the message
>> handler).

>> Here are code snippets to illustrate the problem:

>> Thread code:

>> UINT DownLoadFileThreadProc(LPVOID pParam)
>> {
>>         Download of file

>>         ::PostMessage((HWND) (AfxGetApp()->m_pMainWnd),
>> WM_DOWNLOADFILE, 0, 0);
>> }

>> Message Handler:

>> #define WM_DOWNLOADFILE                 WM_USER+1

>>         ON_MESSAGE(WM_DOWNLOADFILE, OnDownLoadFileMsg)

>> afx_msg LRESULT CSiteGrabberView::OnDownLoadFileMsg(WPARAM wParam,
>> LPARAM lParam)
>> {
>>         AfxMessageBox("OnDownLoadFileMsg here!");
>>         return 0;
>> }



>> >>... I get a compiler error
>> >>stating that the number of parameters in PostMessage is wrong no
>> >>matter how many parameters there are..

>> >Post the line of code you have tried along with the error message.
>> >It's hard to guess what is wrong.

>> >Dave
>> >----
>> >My address is altered to discourage junk mail.
>> >Please post responses to the newsgroup thread,
>> >there's no need for follow-up email copies.



Sun, 19 Aug 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. message passing between threads

2. About Thread and Message Passing

3. Passing messages between threads?

4. Passing messages between MFC threads?

5. thread messages instead of windows messages

6. How to Send messages from worker thread to main thread

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

8. Passing Data From One thread to another

9. Passing parameters to a new thread

10. Passing parameters to a Thread'ed function

11. How to pass params to a thread?

12. RESULT: comp.programming.threads passes 417:11

 

 
Powered by phpBB® Forum Software