PostMessage()在vc ++线程中不与主程序通信 [英] PostMessage() in vc++ thread not talking to main program

查看:75
本文介绍了PostMessage()在vc ++线程中不与主程序通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从visual c ++用户线程向主

窗口发送消息,因此主窗口可以更新窗口上的文本以反映

用户线程正在做什么。我尝试了两种不同的方法,而且
都失败了。我不知道下一步该尝试什么。


+++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ me)

配置线程的PostMessage()的第一个参数与父窗口对话,并设置父级的BEGIN_MESSAGE_MAP来处理

事件。我还仔细检查了第一个参数的有效性,并将它提供给SetWindowText()函数,我可以看到窗口

标题更改。但是,父级中的消息处理程序断点是

从未达到过。以下是一些示例代码:


===来自线程的.cpp文件===


CString theString(" Untitled - mh53");

LPCTSTR aaa =(LPCTSTR)theString;

HWND qqq = :: FindWindow(NULL,aaa);

if(qqq == NULL){exit(1); } //这不会发生

SetWindowText(qqq,(LPCTSTR)" xxxxxx"); //这确实发生了

if(:: PostMessage(qqq,IDC_ARC220_THREAD_CONNECTED,0,0)== 0)

int iii = 1; //失败

else

int iiii = 1; //成功(这发生在调试器中)


===来自主程序的.h文件===


afx_msg LRESULT OnArc220Connected(WPARAM wParam,LPARAM lParam);


===来自主程序的.cpp文件===


BEGIN_MESSAGE_MAP(CMh53View,CFormView的)

ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED,OnArc220Connected)

END_MESSAGE_MAP()


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

方法#2:

配置线程的PostMessage()的第一个参数与线程交换

,然后设置线程的BEGIN_MESSAGE_MAP用于处理

事件并在父节点中调用正确的消息处理程序。使用这种

方法,父进程中的消息处理程序会被调用,但是

程序在逻辑中崩溃以更新窗口文本。这个窗口

文本更新只要从

主程序内部调用就可以完美运行。我不认为主程序的功能就像从线程直接调用



以下是一些示例代码:

$从线程的的.cpp文件b $ b === ===


IMPLEMENT_DYNCREATE(CArc220Thread,的CWinThread)

ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED,

CMh53View :: OnArc220Connected)

BEGIN_MESSAGE_MAP(CArc220Thread,的CWinThread)


....跳过向下过去的多行代码.. 。


if(:: PostMessage(NULL,IDC_ARC220_THREAD_CONNECTED,0,0)== 0)

int iii = 1; //失败

else

int iiii = 1; //成功(这发生在调试器中)

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)

推荐答案

" seattleboatguy" <做***** @ eskimo.com>在消息中写道

news:11 ********************* @ f14g2000cwb.googlegro ups.com
"seattleboatguy" <do*****@eskimo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com
我正在尝试从可视化c ++用户线程向
主窗口发送消息,因此主窗口可以更新窗口上的文本以反映用户线程正在执行的操作。我尝试了两种不同的方法,但都失败了。我不知道下一步该尝试什么。

+++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++线程'的PostMessage()的第一个参数与父窗口交谈,并设置父级的BEGIN_MESSAGE_MAP来处理事件。我通过将它提供给SetWindowText()函数来仔细检查第一个参数的有效性,我可以看到窗口
标题更改。但是,父进程中的消息处理程序断点永远不会到达。以下是一些示例代码:

===来自线程的.cpp文件===

CString theString(" Untitled - mh53");
HWND qqq = :: FindWindow(NULL,aaa);
if(qqq == NULL){exit(1); } //这不会发生
SetWindowText(qqq,(LPCTSTR)" xxxxxx"); //这确实发生了
if(:: PostMessage(qqq,IDC_ARC220_THREAD_CONNECTED,0,0)== 0)
int iii = 1; //失败

int iiii = 1; //成功(这种情况发生在调试器)

===主程序的.h文件中===

AFX_MSG LRESULT OnArc220Connected(WPARAM wParam参数,LPARAM lParam的);

===从
BEGIN_MESSAGE_MAP(CMh53View,的CFormView)/> ON_MESSAGE(IDC_ARC220_THREAD_CONNECTED,OnArc220Connected)
END_MESSAGE_MAP()

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







线程,并设置线程的BEGIN_MESSAGE_MAP来处理
事件并在父线程中调用正确的消息处理程序。使用这种方法,父进程中的消息处理程序会被调用,但是
程序在逻辑中崩溃以更新窗口文本。只要从
主程序内部调用,此窗口
文本更新即可完美运行。我不认为主程序的功能就像直接从线程中调用一样。
以下是一些示例代码:

===来自线程'' cpp文件===

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

...跳过多行代码......

if(:: PostMessage(NULL,IDC_ARC220_THREAD_CONNECTED,0,0)== 0)
int iii = 1; //失败

int iiii = 1; //成功(这在调试器中发生)
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)



这是特定于Windows的。从外观上看,你正在使用MFC。

我建议你问一下


microsoft.public.vc.mfc

-

John Carson


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


2006年1月22日08:15:42 -0800,seattleboatguy < do ***** @ eskimo.com>

写道:
On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" <do*****@eskimo.com>
wrote:
我试图从可视化c ++用户线程发送消息主要的窗口
I am trying to send a message from a visual c++ user-thread to the main
window




这里的人不会帮助你。他们非常喜欢播放我是

比你的游戏更优越 (tm)(r)


试试这些新闻组:


comp.os.ms-windows.programmer.win32

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


祝你好运。


"如果你有一万你销毁的规定

尊重法律。 - 温斯顿丘吉尔



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


JustBoo写道:
JustBoo wrote:
2006年1月22日08:15:42 -0800,seattleboatguy < do ***** @ eskimo.com>
写道:
On 22 Jan 2006 08:15:42 -0800, "seattleboatguy" <do*****@eskimo.com>
wrote:
我正在尝试从visual c ++用户线程发送消息到主要
窗口
I am trying to send a message from a visual c++ user-thread to the main
window



这里的人不会帮助你。他们非常喜欢播放我比你更优越的游戏。 (tm)(r)



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




哦,来吧!知道c ++是否意味着你应该知道vc ++?

我会帮助OP(狡猾地)但我不知道应该是视觉c ++。我怀疑自恋与此有很大关系。



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.


这篇关于PostMessage()在vc ++线程中不与主程序通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆