C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

PostMessage() in vc++ thread not talking to main program

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
seattleboatguy
Guest





PostPosted: Sun Jan 22, 2006 4:15 pm    Post subject: PostMessage() in vc++ thread not talking to main program Reply with quote



I am trying to send a message from a visual c++ user-thread to the main
window, so the main window can update text on the window to reflect
what the user-thread is doing. I have tried 2 different approaches, and
both fail. I don't know what to try next.

+++++++++++++++++++++++++++++++++++++++++++++++++++++

APPROACH #1: (this approach makes the most sence to me)
Configure the first parameter of the thread's PostMessage() to talk to
the parent window, and set up the parent's BEGIN_MESSAGE_MAP to handle
the event. I double-check the validity of that first parameter by also
feeding it to the SetWindowText() function, and I can see the window
title change. But, the message handler break point in the parent is
never reached. Here is some sample code:

=== from the thread's .cpp file ===

CString theString( "Untitled - mh53" );
LPCTSTR aaa = (LPCTSTR) theString;
HWND qqq = ::FindWindow( NULL , aaa);
if (qqq == NULL) { exit(1); } // this does not happen
SetWindowText(qqq, (LPCTSTR) "xxxxxx"); // this does happen
if ( ::PostMessage( qqq, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)

=== from the main program's .h file ===

afx_msg LRESULT OnArc220Connected(WPARAM wParam,LPARAM lParam);

=== from the main program's .cpp file ===

BEGIN_MESSAGE_MAP(CMh53View, CFormView)
ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED, OnArc220Connected)
END_MESSAGE_MAP()

+++++++++++++++++++++++++++++++++++++++++++++++++++++

APPROACH #2:
Configure the first parameter of the thread's PostMessage() to talk to
the thread, and set up the thread's BEGIN_MESSAGE_MAP to handle the
event and call the proper message handler in the parent. With this
approach, the message handler in the parent does get called, but the
program crashes in the logic to update the window text. This window
text update works flawlessly so long as it is called from inside the
main program. I don't think the main program functions like to be
called directly from the thread.
Here is some sample code:

=== from the thread's .cpp file ===

IMPLEMENT_DYNCREATE(CArc220Thread, CWinThread)
ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED,
CMh53View::OnArc220Connected)
BEGIN_MESSAGE_MAP(CArc220Thread, CWinThread)

.... skipping down past many lines of code ...

if ( ::PostMessage( NULL, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)

Back to top
John Carson
Guest





PostPosted: Sun Jan 22, 2006 4:41 pm    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote



"seattleboatguy" <douglas (AT) eskimo (DOT) com> wrote

Quote:
I am trying to send a message from a visual c++ user-thread to the
main window, so the main window can update text on the window to
reflect what the user-thread is doing. I have tried 2 different
approaches, and both fail. I don't know what to try next.

+++++++++++++++++++++++++++++++++++++++++++++++++++++

APPROACH #1: (this approach makes the most sence to me)
Configure the first parameter of the thread's PostMessage() to talk to
the parent window, and set up the parent's BEGIN_MESSAGE_MAP to handle
the event. I double-check the validity of that first parameter by also
feeding it to the SetWindowText() function, and I can see the window
title change. But, the message handler break point in the parent is
never reached. Here is some sample code:

=== from the thread's .cpp file ===

CString theString( "Untitled - mh53" );
LPCTSTR aaa = (LPCTSTR) theString;
HWND qqq = ::FindWindow( NULL , aaa);
if (qqq == NULL) { exit(1); } // this does not happen
SetWindowText(qqq, (LPCTSTR) "xxxxxx"); // this does happen
if ( ::PostMessage( qqq, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)

=== from the main program's .h file ===

afx_msg LRESULT OnArc220Connected(WPARAM wParam,LPARAM lParam);

=== from the main program's .cpp file ===

BEGIN_MESSAGE_MAP(CMh53View, CFormView)
ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED, OnArc220Connected)
END_MESSAGE_MAP()

+++++++++++++++++++++++++++++++++++++++++++++++++++++

APPROACH #2:
Configure the first parameter of the thread's PostMessage() to talk to
the thread, and set up the thread's BEGIN_MESSAGE_MAP to handle the
event and call the proper message handler in the parent. With this
approach, the message handler in the parent does get called, but the
program crashes in the logic to update the window text. This window
text update works flawlessly so long as it is called from inside the
main program. I don't think the main program functions like to be
called directly from the thread.
Here is some sample code:

=== from the thread's .cpp file ===

IMPLEMENT_DYNCREATE(CArc220Thread, CWinThread)
ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED,
CMh53View::OnArc220Connected)
BEGIN_MESSAGE_MAP(CArc220Thread, CWinThread)

... skipping down past many lines of code ...

if ( ::PostMessage( NULL, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)


This is highly Windows-specific. From the look of things, you are using MFC.
I sugggest you ask in

microsoft.public.vc.mfc


--
John Carson



Back to top
JustBoo
Guest





PostPosted: Sun Jan 22, 2006 5:04 pm    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote



On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" <douglas (AT) eskimo (DOT) com>
wrote:

Quote:
I am trying to send a message from a visual c++ user-thread to the main
window

People here will not help you. They quite like playacting the "I'm
More Superior Than You Game" (tm)(r)

Try these newsgroups:

comp.os.ms-windows.programmer.win32
comp.os.ms-windows.programmer.tools.mfc

Good Luck.

"If you have ten thousand regulations you destroy
all respect for the law." - Winston Churchill

Back to top
Shark
Guest





PostPosted: Sun Jan 22, 2006 10:33 pm    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote

JustBoo wrote:
Quote:
On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" wrote:

I am trying to send a message from a visual c++ user-thread to the main
window

People here will not help you. They quite like playacting the "I'm
More Superior Than You Game" (tm)(r)

Oh comeon! Does knowing c++ mean you are supposed to know vc++ as well?
I'd help the OP (slyly) but I just don't know enought visual c++. I
doubt narcissism has so much to do with it.


Back to top
Jim Langston
Guest





PostPosted: Mon Jan 23, 2006 5:01 am    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote


"JustBoo" <JustBoo (AT) BooWho (DOT) com> wrote

Quote:
On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" wrote:

I am trying to send a message from a visual c++ user-thread to the main
window

People here will not help you. They quite like playacting the "I'm
More Superior Than You Game" (tm)(r)

Since you didn't help them either, that puts you in the same catagory
JustBoo



Back to top
John Carson
Guest





PostPosted: Mon Jan 23, 2006 5:44 am    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote

"Jim Langston" <tazmaster (AT) rocketmail (DOT) com> wrote

Quote:
"JustBoo" <JustBoo (AT) BooWho (DOT) com> wrote in message
news:5be7t15fscrlnrevu3kr5fpvn8jcs89auu (AT) 4ax (DOT) com...
On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" wrote:

I am trying to send a message from a visual c++ user-thread to the
main window

People here will not help you. They quite like playacting the "I'm
More Superior Than You Game" (tm)(r)

Since you didn't help them either, that puts you in the same catagory
JustBoo


I did help the OP by directing him to the most appropriate newsgroup for his
question (a question to which I did not know the answer). Questions like
that are bread and butter on microsoft.public.vc.mfc and a solution is all
but guaranteed. The question involved MFC message maps, which every MFC
programmer knows about by necessity, but non-MFC programmers typically
don't.

JustBoo, by contrast, directed the OP to one newsgroup that has very little
MFC discussion (comp.os.ms-windows.programmer.win32) and one that is all but
dead (comp.os.ms-windows.programmer.tools.mfc).

JustBoo is in fact an A-grade hypocrite. The ratio of smart-arse insults to
helpful advice is higher in his posts than in the posts of 99% of the
contributors to this newsgroup.

--
John Carson



Back to top
Default User
Guest





PostPosted: Mon Jan 23, 2006 6:12 am    Post subject: Re: PostMessage() in vc++ thread not talking to main program Reply with quote

John Carson wrote:


Quote:
JustBoo is in fact an A-grade hypocrite. The ratio of smart-arse
insults to helpful advice is higher in his posts than in the posts of
99% of the contributors to this newsgroup.

I guess I'm not sure why Boo isn't killfiled by most. It was pretty
obvious by about post 3 that he was just here to troll.



Brian

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.