 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
seattleboatguy Guest
|
Posted: Sun Jan 22, 2006 4:15 pm Post subject: PostMessage() in vc++ thread not talking to main program |
|
|
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
|
Posted: Sun Jan 22, 2006 4:41 pm Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
"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
|
Posted: Sun Jan 22, 2006 5:04 pm Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
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
|
Posted: Sun Jan 22, 2006 10:33 pm Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
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
|
Posted: Mon Jan 23, 2006 5:01 am Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
"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
|
Posted: Mon Jan 23, 2006 5:44 am Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
"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
|
Posted: Mon Jan 23, 2006 6:12 am Post subject: Re: PostMessage() in vc++ thread not talking to main program |
|
|
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 |
|
 |
|
|
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
|
|