主线程能不能给子线程sendmessage?(50分)

K

kiths

Unregistered / Unconfirmed
GUEST, unregistred user!
我在主线程中创建了10个线程用数组保存起来
SaveThreadInfo=record
Savethread:TSaveTaskThread;
Used:boolean;
end;
SaveThreadArray :array[1..9] of SaveThreadInfo;
for i:=1 to 9do
//创建 所有 线程
begin
SaveThreadArray.Savethread:=TSaveTaskThread.create(i);
SaveThreadArray.Used:=False;
end;
Sendmessage(SaveThreadArray[aThreadID].Savethread.Handle,WM_USER+1,1,0);
基本上就是这样,但子线程收不到?
哪位大侠有这个经验?请赐教
分数不多了,意思意思吧
 
当然可以了,不过消息好像只能发到窗口啊
 
用postthreadmessage给线程发,当然子线程中要建立消息循环处理消息
 
线程好像有独立的消息循环,如果要获得消息,还得要能够让线程处理消息吧。
这个。。。。。我也没有研究过,不太清楚。不过我试过,用线程中的窗体可以发送接收消息。不知道是不是可以往线程里面写一个WinProc的处理函数?呵呵,建议楼主研究一下。
 
你的消息循环处理是不是指消息处理函数?
 
call PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create the message queue. Set the event, to indicate that the thread is ready to receive posted messages.
翻译:
调用 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) 强制系统为线程创建
消息队列,同时设置事件,表明线程已经准备好接收消息
给线程发送消息,用PostThreadMessage
Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
翻译:
调用PostThreadMessage ,如果失败,调用Sleep,然后再调用PostThreadMessage
知道它返回成功
 
可以给线程发消息,不过线程要有消息处理机制
线程的消息处理机制如下
procedure TMyThread.Execute;
var
msg: TMsg;
dwHandleSignaled: DWORD;
begin
{ Place thread code here }
FreeOnTerminate := true;
while truedo
begin
if not PeekMessage( msg, 0, 0, 0, PM_REMOVE ) then
begin
dwHandleSignaled := MsgWaitForMultipleObjects(1, FQuitEvent, False,
INFINITE, QS_ALLINPUT);
case dwHandleSignaled of
WAIT_OBJECT_0: exit;
//QuitEvent signaled!
WAIT_OBJECT_0 + 1: continue;
//New message was received.
else
exit;
//This case should never occur.
end;
end;
if msg.hwnd <> 0{NULL} then
begin
TranslateMessage(msg);
DispatchMessage(msg);
Continue;
end;

case msg.message of
MSG_1: ...
MSG_2: ...
.
.
.
MSG_N: ...
end;
end;
end;
 
to:ss2000
WAIT_OBJECT_0
是不是在terminate时候触发?
 
这是我程序中的一段代码
const
um_ontimer=wm_user+259;

um_quitthread=wm_user+261;
procedure Tmythread.execute;
begin
while truedo
begin
if waitmessage then
begin
if peekMessage(M,0,0,0,PM_REMOVE) then
if m.message=um_ontimer then
begin
//处理
end;
if m.message=um_quitthread then
break;
end;
end;
end;
//发消息
postthreadmessage(mythread.ThreadID,um_ontimer,0,0);
 
to kiths:
不是,FQuitEvent是一个全局的事件,当主程序要退出的时候,置位该事件,这样,我的所有子线程都会随时判断该事件,一旦该事件发生,就知道程序需要退出了,即子线程自动终止。是我在应用中的一个小技巧而已,这样就不必每个子线程都通知一次(有时候都不知道有多少个子线程)。
 
to ss2000:
我感觉迷糊的写法好像比较简单,你的写法不光在线程中适用,你能告诉我你的写法具体有什么好处吗?或者你觉得迷糊的写法怎么样?
 
迷糊的写法是只能等待消息。
我的写法是既可以等待消息触发,又可以用事件触发。
通用性更强一些。特别是你的线程需要判断事件的时候。
又比如,当你的主程序要退出时,你需要给你的10个线程挨个发退出消息,
而我的办法只需要在主线程一句就可以。
SetEvent(FQuitEvent);
特别是你有不同类型的子线程,个数又不确定,甚至都没有保存子线程的变量,就非常有用了。
 
to ss2000:
多谢指点!
 
to ss2000:
PeekMessage( msg, 0, 0, 0, PM_REMOVE )
TranslateMessage(msg);
DispatchMessage(msg);
PeekMessage( msg, 0, 0, 0, PM_REMOVE )
再对msg处理这样两次PeekMessage会不会变成不同的msg?
 
PeekMessage( msg, 0, 0, 0, PM_REMOVE )
是取消息的函数,每次都是从系统取
你的代码是什么意思?或者你想问什么?
 
我的代码是说你的代码的执行流程,因为你有两个continue;
还有你的
TranslateMessage(msg);
DispatchMessage(msg);
做什么用啊?
还有主线程怎么保证子线程已经收到消息?
 
if msg.hwnd <> 0{NULL} then
这肯定不是给线程的消息,是给线程的窗口的,所以线程本身不必处理。
TranslateMessage(msg);
DispatchMessage(msg);
你看看帮助吧。
还有主线程怎么保证子线程已经收到消息?
没有办法保证。
通常都会收到,可以不考虑。
否则你自己想办法,做一个应答机制。
 
我的线程没有窗口哦
我用你昨天的方法作了,但是线程已进去就退出了,我定义了一个全局事件,在APPLICATION.Initialize后CREATEEVENT(FQUITEVENT)
 
FQuitEvent: THandle
FQuitEvent:= CreateEvent(nil,true,false,nil);
 
我就是这么写的,可是收不到消息
主线程中的调用
PostThreadMessage(SaveThreadArray[aThreadID].Savethread.Handle,WM_TASKMSG,integer(@TaskMSG),0);
返回false
 

Similar threads

S
回复
0
查看
959
SUNSTONE的Delphi笔记
S
S
回复
0
查看
779
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部