Sending message from working non-gui thread to the main window

丶灬走出姿态 提交于 2020-01-15 10:12:27

问题


I'm using WinApi.

Is SendMessage/PostMessage a good, thread safe method of communicating with the main window? Suppose, the working thread is creating a bitmap, that must be displayed on the screen. The working thread allocates a bitmap, sends a message with a pointer to this bitmap and waits until GUI thread processes it (for example using SendMessage). The working thread shares no data with other threads.

Am I running into troubles with such design?

Are there any other possibilities that do not introduce thread synchronizing, locking etc. ?


回答1:


This is a decent method of thread synchronisation, and should be fine, so long as you're careful that:

  • either the main thread keeps the bitmap and the worker thread starts a new one, or vice versa, so that after SendMessage returns only one thread has access to the bitmap.
  • you don't accidentally change the SendMessage to a PostMessage later, forgetting that it is providing your synchronisation as well as the inter-thread communication.


来源:https://stackoverflow.com/questions/2562689/sending-message-from-working-non-gui-thread-to-the-main-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!