{"id":110568,"date":"2024-11-25T07:00:00","date_gmt":"2024-11-25T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110568"},"modified":"2024-11-25T08:33:52","modified_gmt":"2024-11-25T16:33:52","slug":"20241125-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20241125-00\/?p=110568","title":{"rendered":"How can I know when a window has processed a message that I posted to it?"},"content":{"rendered":"<p>A customer was writing diagnostic code to monitor another application. They found that under certain conditions (which they can detect by other means), the program would stop responding to incoming posted messages, though it would respond to incoming sent messages.\u00b9 Is there version of <code>Post\u00adMessage<\/code> that puts the message in the message queue and either waits for the message to be retrieved (like <code>Send\u00adMessage<\/code>) or calls you back when the retrieval occurs (like <code>Send\u00adMessage\u00adCallback<\/code>)?<\/p>\n<p>No, there is no such version. When a message is posted, it is placed in destination queue, and that&#8217;s the end of it. There is no further tracking of the message.<\/p>\n<p>If you can change the target program, you can post it a custom message like <code>WM_<wbr \/>HEARTBEAT<\/code> and implement the handler for that message by posting an acknowledgement back.<\/p>\n<pre>\/\/ Monitoring program posts a heartbeat\r\nPostMessage(hwndAppBeingMonitored, WM_HEARTBEAT,\r\n    (WPARAM)hwndMonitor,\r\n    MAKELPARAM(replyMessage, replyMessageData));\r\n\r\n\/\/ Program being monitored replies with a heartbeat\r\ncase WM_HEARTBEAT:\r\n    PostMessage((HWND)wParam, LOWORD(lParam),\r\n                HIWORD(lParam), 0);\r\n<\/pre>\n<p>If you cannot make changes to the app being monitored, you could install a <code>WH_<wbr \/>GET\u00adMESSAGE<\/code> hook onto the UI thread of the app you are monitoring, post the window a harmless message like <code>WM_<wbr \/>NULL<\/code>, and have your hook respond when it sees that message.<\/p>\n<p>Another option is to post a harmless message that has some observable effect, like <code>WM_<wbr \/>CONTEXT\u00adMENU<\/code> and then use accessibility or UI Automation to detect when the menu appears. (You could also use accessibility or UI Automation to dismiss the menu.)<\/p>\n<p>\u00b9 This can happen if the thread is inside its own <code>Send\u00adMessage<\/code> call to another thread. While waiting for <code>Send\u00adMessage<\/code> to complete, the thread can process inbound sent messages, but inbound posted messages go into the queue and are not processed until the thread calls <code>Get\u00adMessage<\/code> or <code>Peek\u00adMessage<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Best would be to have it tell you.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-110568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Best would be to have it tell you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=110568"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110568\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=110568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}