有如下情况:在主线程中创建一个子线程,主线程 通过 发送消息 来通知子线程,我见过有两种方式:
1、子线程中procedure TThread.WndProc(var Message: TMessage);
begin
DefWindowProc(FWnd, Message.Msg, Message.WParam, Message.LParam);
end;procedure TThread.Execute;
begin
{$IFDEF DELPHI_6_UP}
FWnd := Classes.AllocateHWnd(WndProc);
{$ELSE}
FWnd := AllocateHWnd(WndProc);
{$ENDIF}
try
while not Terminated do
begin
try
GetMessage(Msg, FWnd, 0, 0); if //想要自己处理的消息
begin
//相关操作
end
else if not Terminated then
DispatchMessage(Msg);
except
end;
end;
finally
{$IFDEF DELPHI_6_UP}
Classes.DeallocateHWnd(FWnd);
{$ELSE}
DeallocateHWnd(FWnd);
{$ENDIF}
end;
end;
主线程: 用 PostMessgae 通知子线程2、子线程中:procedure TThread.Execute;
begin
while not Terminated do
begin
try
GetMessage(Msg, 0, 0, 0); //根据 不同情况 ,做不同事情 except
end;
end;
end;
主线程:用PostThreadMessage通知子线程
上面两种方式,那种比较好?比较合理,高效,健壮?或有更好的方式?
1、子线程中procedure TThread.WndProc(var Message: TMessage);
begin
DefWindowProc(FWnd, Message.Msg, Message.WParam, Message.LParam);
end;procedure TThread.Execute;
begin
{$IFDEF DELPHI_6_UP}
FWnd := Classes.AllocateHWnd(WndProc);
{$ELSE}
FWnd := AllocateHWnd(WndProc);
{$ENDIF}
try
while not Terminated do
begin
try
GetMessage(Msg, FWnd, 0, 0); if //想要自己处理的消息
begin
//相关操作
end
else if not Terminated then
DispatchMessage(Msg);
except
end;
end;
finally
{$IFDEF DELPHI_6_UP}
Classes.DeallocateHWnd(FWnd);
{$ELSE}
DeallocateHWnd(FWnd);
{$ENDIF}
end;
end;
主线程: 用 PostMessgae 通知子线程2、子线程中:procedure TThread.Execute;
begin
while not Terminated do
begin
try
GetMessage(Msg, 0, 0, 0); //根据 不同情况 ,做不同事情 except
end;
end;
end;
主线程:用PostThreadMessage通知子线程
上面两种方式,那种比较好?比较合理,高效,健壮?或有更好的方式?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货