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 ( :ostMessage( 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 ( :ostMessage( NULL, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)
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 ( :ostMessage( 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 ( :ostMessage( NULL, IDC_ARC220_THREAD_CONNECTED, 0, 0) == 0 )
int iii = 1; // failed
else
int iiii = 1; // success (this happens in debugger)