How to exit a thread's message loop?

前端 未结 2 550
难免孤独
难免孤独 2021-02-02 16:35

A background-thread can be configured to receive window messages. You would post messages to the thread using PostThreadMessage. What\'s the correct way to exit that message loo

2条回答
  •  孤街浪徒
    2021-02-02 17:25

    Using PostThreadMessage is not necessarily incorrect. Raymond's article that you linked to says:

    Because the system tries not to inject a WM_QUIT message at a "bad time"; instead it waits for things to "settle down" before generating the WM_QUIT message, thereby reducing the chances that the program might be in the middle of a multi-step procedure triggered by a sequence of posted messages.

    If the concerns outlined here do not apply to your message queue, then call PostThreadMessage with WM_QUIT and knock yourself out. Otherwise you'll need to create a special signal, i.e. a user-defined message, that allows you to call PostQuitMessage from the thread.

提交回复
热议问题