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

S

seattleboatguy

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)
 
J

John Carson

seattleboatguy said:
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
 
J

JustBoo

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
 
S

Shark

JustBoo said:
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.
 
J

Jim Langston

JustBoo said:
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
 
J

John Carson

Jim Langston said:
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.
 
D

Default User

John Carson wrote:

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
 
J

Jim Langston

John Carson said:
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.

I tend to agree. Telling someone that their question is OT and directing
them to the proper newsgroup is, in fact, helping them.
 
J

Jacek Dziedzic

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

Shit, man, I don't want to sound like /dev/null again, but this
newsgroup IS NOT about visual c++. By asking platform-specific
questions here you will either
a) get pointed to a more appropriate newsgroup (if you're lucky)
b) get a quick hint towards a solution (if you're very lucky,
but then the person answering might get flamed),
c) be flamed, which is fine, since you're OT.

I'd go for a) and try again with a
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Can /dev/null reiterate responses like that? I doubt it.

People around here are very sensitive about the topicality
of the newsgroup. They don't enjoy flaming others, they don't
feel like "standard C++ is Important, everything else is not",
it's just the signal-to-noise ratio gets annoyingly low, mostly
because of OT posters. Get it.

HTH (sincerely),
- J.
 
J

JustBoo

Since you didn't help them either, that puts you in the same catagory
JustBoo
What are you talking about? Since you didn't read my post I will paste
it in full.

<quote>
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
</quote>

Notice the section that says "Try these newsgroups:" Did you notice
that? Did you? And of course you displayed the very behavior I
describe in my first line. Just like clockwork.

However beautiful the strategy, you should occasionally
look at the results. - Winston Churchill
 
J

JustBoo

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).

That is your *opinion.* I perceive comp.std.c++ as dead, others do
not. Opinion. I'll tell you one thing if someone asks a MFC question
in "Win32" they are not vilified and metaphorically chased out by a
myopic crowd of Superior wannabe villagers with pitch-forks and
torches. We try to answer what we can.

I directed him to where *I* felt he would get the best help. You are
free to do the same.
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.

Once again, opinion. And as we all know usenet is *nothing* but
opinion. Take *any* subject, well, in the Standard for example, and
people will argue, oh I mean debate, oh I mean give you their
opinion, until the end of time.

I use the K&R style; so there!

"Socialism is a philosophy of failure, the creed of
ignorance, and the gospel of envy, its inherent
virtue is the equal sharing of misery."
- Winston Churchill
 
J

JustBoo

I tend to agree. Telling someone that their question is OT and directing
them to the proper newsgroup is, in fact, helping them.

Uh, hello. That is *exactly* what I did. Oh no, the bovine stare
again.

The philosophy exam was a piece of cake which was a
bit of a surprise, actually, because I was expecting
some questions on a sheet of paper.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top