{"id":21393,"date":"2008-08-01T10:00:00","date_gmt":"2008-08-01T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/08\/01\/i-warned-you-the-dangers-of-attaching-input-queues\/"},"modified":"2008-08-01T10:00:00","modified_gmt":"2008-08-01T10:00:00","slug":"i-warned-you-the-dangers-of-attaching-input-queues","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080801-00\/?p=21393","title":{"rendered":"I warned you: The dangers of attaching input queues"},"content":{"rendered":"<p><P>\nSome people didn&#8217;t take to heart my cautions on the subject\nof attached input queues, item number five on the list of\n<A HREF=\"https:\/\/channel9.msdn.com\/Showpost.aspx?postid=116704\">\nfive things every Win32 programmer should know<\/A>.\nAnd then they find that their application stops responding.\n<\/P>\n<PRE>\n<I>\/\/ Code in italics is wrong\nvoid TryToStealFocus(HWND hwnd)\n{\n  \/\/ First try plain SetForegroundWindow\n  SetForegroundWindow(hwnd);\n  HWND hwndFG = GetForegroundWindow();\n  if (hwndFG == hwnd) return;<\/p>\n<p>  \/\/ That didn&#8217;t work &#8211; if the foreground window belongs\n  \/\/ to another thread, attach to that thread and try again\n  DWORD dwCurrentThread = GetCurrentThreadId();\n  DWORD dwFGThread = GetWindowThreadProcessId(hwndFG, NULL);\n  if (dwFGThread == dwCurrentThread) return;<\/p>\n<p>  AttachThreadInput(dwCurrentThread, dwFGThread, TRUE);\n  SetForegroundWindow(hwnd); \/\/ hangs here\n  AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);\n}<\/I>\n<\/PRE>\n<P>\nTheir customer feedback data shows that this function often\nhangs at the second call to <CODE>SetForegroundWindow<\/CODE>.\nMy exercise for you is to explain why.\n(Here&#8217;s\n<A HREF=\"http:\/\/groups.google.com\/groups?selm=8f573ed4-6380-410a-ba9b-921b94f2e33d@b40g2000prf.googlegroups.com\">\nsomeone else with the same problem<\/A>.)\n<\/P>\n<P>\n(Note that both of these customers are trying to circumvent\nthe foreground lock timeout so that they can steal focus and shove\na dialog box in the user&#8217;s face.)\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some people didn&#8217;t take to heart my cautions on the subject of attached input queues, item number five on the list of five things every Win32 programmer should know. And then they find that their application stops responding. \/\/ Code in italics is wrong void TryToStealFocus(HWND hwnd) { \/\/ First try plain SetForegroundWindow SetForegroundWindow(hwnd); HWND [&hellip;]<\/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-21393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Some people didn&#8217;t take to heart my cautions on the subject of attached input queues, item number five on the list of five things every Win32 programmer should know. And then they find that their application stops responding. \/\/ Code in italics is wrong void TryToStealFocus(HWND hwnd) { \/\/ First try plain SetForegroundWindow SetForegroundWindow(hwnd); HWND [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21393","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=21393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21393\/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=21393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=21393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=21393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}