【问题标题】:How to exit a thread's message loop?如何退出线程的消息循环?
【发布时间】:2012-05-14 03:47:16
【问题描述】:

后台线程可以配置为接收窗口消息。您可以使用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 消息并返回 false 的正确方法是什么。

我们已经知道that the incorrect way to post a WM_QUIT message 是调用:

PostThreadMessage(nThreadId, WM_QUIT, 0, 0);

甚至还有examples out there where people employ this approach。来自 MSDN:

不要使用PostMessage 函数发布WM_QUIT 消息;使用PostQuitMessage

正确的方式是“某人”拨打PostQuitMessagePostQuitMessage is a special function,设置与消息队列关联的特殊标志,以便GetMessage 在适当的时候合成WM_QUIT 消息。

如果这是一个与“window”相关联的消息循环,那么标准设计模式是当窗口被销毁并收到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_DESTROYis sent by the Window Manager. It is sent when someone calls DestroyWindow. It is wrong to just post 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;

但是有没有规范的方法可以退出线程的消息循环?


但不要这样做

事实证明,每个人最好不要使用无窗口消息队列。如果您没有发送消息的窗口,很多事情可能会在无意中被巧妙地破坏。

Instead allocate hidden window(例如,使用 Delphi 的 thread-unsafe AllocateHwnd)并使用普通的旧 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;

【问题讨论】:

  • 一旦你有一个特殊的消息,为什么还要麻烦 PostQuitMessage?离开那里。
  • 如果线程中没有创建窗口,调用TranslateMessage - DispatchMessage 的目的是什么?
  • @Dialectus break 会更容易!
  • 不像加州酒店! ;-)
  • @Sertac 正确,documentation 明确表示调用 GetMessage 会创建消息队列。

标签: multithreading delphi winapi message-loop


【解决方案1】:

没有规范的方法;没有佳能。您可以通过发布wm_Quit 消息让GetMessage 返回零,然后通过调用PostQuitMessage 来实现。您知道何时以及如何做到这一点取决于您。

响应wm_Destroy 这样做很常见,但这只是因为退出程序的常见方法是关闭窗口。如果您没有要关闭的窗口,请选择其他方式。您的wm_PleaseEndYourself 想法很好。

您甚至不必发送消息。您可以使用一些可等待的对象,例如事件或信号量,然后使用MsgWaitForMultipleObjects 来检测它是否在等待新消息的同时发出信号。

您甚至不必等待GetMessage 返回零。如果您已经知道线程需要停止,那么您可以完全停止处理消息。有很多方法可以退出循环。您可以使用exitbreakraise,甚至goto,或者您可以设置一个标志,以便在循环条件中与GetMessage的返回值一起检查。

另请注意,GetMessage 在失败时返回 -1,作为非零值将被解释为真。如果GetMessage 失败,您可能不想继续您的消息循环,因此请检查GetMessage(...) > 0,或者像the documentation recommends 那样做。

【讨论】:

  • 啊,我很困惑,因为GetMessage 返回一个BOOL。我会用一个关于错误方法的注释来更新问题。
【解决方案2】:

使用PostThreadMessage 不一定是错误的。您链接到的雷蒙德的文章说:

因为系统试图在“糟糕的时间”不注入 WM_QUIT 消息;相反,它会在生成 WM_QUIT 消息之前等待事情“稳定下来”,从而减少程序可能处于由一系列发布消息触发的多步骤过程中间的可能性。

如果此处列出的问题不适用于您的消息队列,请致电PostThreadMessageWM_QUIT 并让自己失望。否则,您需要创建一个特殊信号,即用户定义的消息,允许您从线程中调用 PostQuitMessage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 2021-07-05
    • 1970-01-01
    • 2011-06-16
    • 2012-06-11
    • 1970-01-01
    • 2010-09-18
    • 2013-12-06
    相关资源
    最近更新 更多