窗口消息循环,线程消息循环,区别???

okmnji79513 okmnji79513 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有如下情况:在主线程中创建一个子线程,主线程 通过 发送消息 来通知子线程,我见过有两种方式:
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通知子线程


上面两种方式,那种比较好?比较合理,高效,健壮?或有更好的方式?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
okmnji79513
Windows SDK/API 1,184 成员 22,326 内容