子线程是否可以向父线程发送一个自定义的消息,如果能,怎么发?(300分)(300分)

  • 主题发起人 主题发起人 meckyhan
  • 开始时间 开始时间
子线程可以向父线程发送一个自定义的消息,<br><br>================================= 子线程 ==============================<br>type<br>&nbsp; TComThread = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; Win_Caller: HWND; &nbsp; &nbsp; &nbsp;// 副线程传来的窗口句柄<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(winHwnd: HWND);<br>&nbsp; end;<br><br>implementation<br>constructor TComThread.Create(winHwnd: HWND);<br>begin<br>&nbsp; &nbsp;inherited Create(True); &nbsp; &nbsp;// 调用系统原有的Create,并且使线程挂起<br>&nbsp; &nbsp;Win_Caller:= winHwnd; &nbsp; &nbsp; &nbsp;// 保存调用线程的窗口的句柄<br>&nbsp; &nbsp;Resume; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 开始执行线程<br>end;<br><br>procedure TComThread.Execute;<br>begin<br>&nbsp; &nbsp;FreeOnTerminate:= True; &nbsp; &nbsp;// 在线程执行完后,自动释放线程对象所占用的资源<br>&nbsp; &nbsp;// do something<br>&nbsp; &nbsp;SendMessage(win_caller, UM_CNTDEVERR, LM_Num, -1);<br>&nbsp; &nbsp;// do something<br>end;<br>================================= 父线程 ==============================<br>type<br>&nbsp; a123 = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; { 这里加入自定义隐藏window的消息处理过程 }<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; end;<br><br>implementation<br><br>procedure a123.Execute;<br>Var <br>&nbsp; SubThread: TComThread;<br>&nbsp; WinHandle: hwnd;<br>begin<br>&nbsp;{ 在这里使用Win API函数创建隐藏的windows,并获取其句柄<br>&nbsp; &nbsp; &nbsp;RegisterClass(); // RegisterClassEx() &nbsp; // 注册这个window 的消息处理过程<br>&nbsp; &nbsp; &nbsp;WinHandle:= CreateEindow(); //CreateEindowEx()<br>&nbsp; &nbsp;}<br>&nbsp; SubThread:= TComThread.Create( WinHandle); <br>{<br>&nbsp;注销前面注册的Window &nbsp;<br>}<br>end;<br><br>以上父进程的代码中不是完整的,只是给你提供一个实现的方法,在delphi自带的<br>源代码中的非可视类也是通过定义隐藏的window 的方式来接收消息的。<br>
 
你也可以通过在建立线程的时候,传入一个父线程中的程序的指针实现。。。[:)]
 
接受答案了.
 
后退
顶部