后台线程可以配置为接收窗口消息。您将使用PostThreadMessage
. 退出该消息循环的正确方法是什么?
背景
在您可以将消息发布到后台线程之前,该线程需要确保通过调用创建消息队列PeekMessage
:
procedure ThreadProcedure;
var
msg: TMsg;
begin
//Call PeekMessage to force the system to create the message queue.
PeekMessage(msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
end;
现在外界可以向我们的线程发布消息:
PostThreadMessage(nThreadID, WM_ReadyATractorBeam, 0, 0);
我们的线程位于一个GetMessage
循环中:
procedure ThreadProcedure;
var
msg: TMsg;
begin
//Call PeekMessage to force the system to create the message queue.
PeekMessage(msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//Start our message pumping loop.
//GetMessage will return false when it receives a WM_QUIT
// GetMessage can return -1 if there's an error
// Delphi LongBool interprets non-zero as true.
// If GetMessage *does* fail, then msg will not be valid.
// We want some way to handle that.
//Invalid:
//while (GetMessage(msg, 0, 0, 0)) do
//Better:
while LongInt(GetMessage(msg, 0, 0, 0)) > 0 do
begin
case msg.message of
WM_ReadyATractorBeam: ReadyTractorBeam;
// No point in calling Translate/Dispatch if there's no window associated.
// Dispatch will just throw the message away
// else
// TranslateMessage(Msg);
// DispatchMessage(Msg);
// end;
end;
end;
GetMessage
我的问题是接收WM_QUIT
消息并返回错误的正确方法是什么。
我们已经了解到,发布消息的错误方式WM_QUIT
是调用:
PostThreadMessage(nThreadId, WM_QUIT, 0, 0);
甚至还有一些人们采用这种方法的例子。来自 MSDN:
不要使用PostMessage函数发布WM_QUIT消息;使用PostQuitMessage。
正确的方法是让“某人”打电话PostQuitMessage
。PostQuitMessage
是一个特殊函数,它设置与消息队列关联的特殊标志,以便在适当的时候GetMessage
合成消息。WM_QUIT
如果这是一个与“窗口”相关联的消息循环,那么标准设计模式是当窗口被销毁并WM_DESTROY
接收到消息时,我们捕获它并调用PostQuitMessage
:
procedure ThreadProcedure;
var
msg: TMsg;
begin
//Call PeekMessage to force the system to create the message queue.
PeekMessage(msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//Start our message pumping loop.
//GetMessage will return false when it receives a WM_QUIT
while Longint(GetMessage(msg, 0, 0, 0)) > 0 do
begin
case msg.message of
WM_ReadyATractorBeam: ReadyTractorBeam;
WM_DESTROY: PostQuitMessage(0);
end;
end;
end;
问题是WM_DESTROY
由窗口管理器发送的。当有人来电时发送DestroyWindow
。只发帖是错误的WM_DESTROY
。
现在我可以合成一些人工WM_PleaseEndYourself
信息:
PostThreadMessage(nThreadID, WM_PleaseEndYourself, 0, 0);
然后在我的线程的消息循环中处理它:
procedure ThreadProcedure;
var
msg: TMsg;
begin
//Call PeekMessage to force the system to create the message queue.
PeekMessage(msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//Start our message pumping loop.
//GetMessage will return false when it receives a WM_QUIT
while Longint(GetMessage(msg, 0, 0, 0)) > 0 do
begin
case msg.message of
WM_ReadyATractorBeam: ReadyTractorBeam;
WM_PleaseEndYourself: PostQuitMessage(0);
end;
end;
end;
但是有没有一种规范的方法可以退出线程的消息循环?
但不要这样做
事实证明,不使用无窗口消息队列对每个人都更好。如果您没有发送消息的窗口,很多事情可能会在无意中被巧妙地破坏。
而是分配隐藏窗口(例如,使用 Delphi 的线程- unsafe AllocateHwnd
)并使用普通的 old 向其发布消息PostMessage
:
procedure TMyThread.Execute;
var
msg: TMsg;
begin
Fhwnd := AllocateHwnd(WindowProc);
if Fhwnd = 0 then Exit;
try
while Longint(GetMessage(msg, 0, 0, 0)) > 0 do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
finally
DeallocateHwnd(Fhwnd);
Fhwnd := 0;
end;
end;
我们可以有一个普通的旧窗口过程来处理消息:
WM_TerminateYourself = WM_APP + 1;
procedure TMyThread.WindowProc(var msg: TMessage);
begin
case msg.Msg of
WM_ReadyATractorBeam: ReadyTractorBeam;
WM_TerminateYourself: PostQuitMessage(0);
else
msg.Result := DefWindowProc(Fhwnd, msg.msg, msg.wParam, msg.lParam);
end;
end;
当你想让线程完成时,你告诉它:
procedure TMyThread.Terminate;
begin
PostMessage(Fhwnd, WM_TerminateYourself, 0, 0);
end;