{"id":108904,"date":"2023-10-18T07:00:00","date_gmt":"2023-10-18T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108904"},"modified":"2023-10-18T07:12:40","modified_gmt":"2023-10-18T14:12:40","slug":"20231018-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231018-00\/?p=108904","title":{"rendered":"How can I check if I&#8217;m on a DispatcherQueue&#8217;s thread if I can&#8217;t call HasThreadAccess?"},"content":{"rendered":"<p>A customer had code that used the <code>Dispatcher\u00adQueue.<wbr \/>Has\u00adThread\u00adAccess<\/code> property, but found that the code crashed when running on Windows Server 2019 systems,\u00b9 because the <code>Has\u00adThread\u00adAccess<\/code> property wasn&#8217;t added until Windows Server Version 1903.<\/p>\n<p>They wondered if there was a way to find out whether you are running on a <code>Dispatcher\u00adQueue<\/code>&#8216;s thread without using the <code>Has\u00adThread\u00adAccess<\/code> property?<\/p>\n<p>Well, let&#8217;s browse around the members of <code>Dispatcher\u00adQueue<\/code>:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Member<\/th>\n<th>Available in Server 2019?<\/th>\n<\/tr>\n<tr>\n<td>GetForCurrentThread<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>CreateTimer<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>TryEnqueue<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>ShutdownStaring<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>ShutdownCompleted<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>HasThreadAccess<\/td>\n<td>No<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>It occurred to me that you can see if a particular <code>Dispatcher\u00adQueue<\/code> belongs to the current thread by simply reversing the question: Ask the current thread for its <code>Dispatcher\u00adQueue<\/code> and see if it&#8217;s the one you were given. This relies on the fact that each thread can have at most one <code>Dispatcher\u00adQueue<\/code>.<\/p>\n<pre>\/\/ Alternate version that simulates HasThreadAccess\r\n\/\/ on Windows Server 2019.\r\n\r\n\/\/ C++\/WinRT\r\nbool DispatcherQueueHasThreadAccess(DispatcherQueue const&amp; q)\r\n{\r\n    ASSERT(q != nullptr); \/\/ caller should have checked this first\r\n    return q == DispatcherQueue::GetForCurrentThread();\r\n}\r\n<\/pre>\n<p>The customer reported back that it worked great.<\/p>\n<p>A lot of computer programming is just looking at the tools you have available in your toolbox and seeing whether you can combine them in an interesting way to accomplish your goal.<\/p>\n<p>\u00b9 Still in extended support until 2029!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking at the tools you have available.<\/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-108904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Looking at the tools you have available.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108904","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=108904"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108904\/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=108904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}