{"id":44583,"date":"2015-02-26T07:00:00","date_gmt":"2015-02-26T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/02\/26\/regnotifychangekeyvalue-sucks-less\/"},"modified":"2019-03-13T12:13:20","modified_gmt":"2019-03-13T19:13:20","slug":"20150226-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150226-00\/?p=44583","title":{"rendered":"RegNotifyChangeKeyValue sucks less"},"content":{"rendered":"<p>One of the gotchas of the <code>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/code> function is that <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/02\/22\/536920.aspx\">the notification registration has thread affinity<\/a>. This is a problem if you want the notification registration to outlive the thread that generated it. In particular, if you register the notification from a nonpersistent thread pool thread, you get into an infinite loop: <\/p>\n<ol>\n<li>Thread pool task calls     <code>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/code>,     and waits for the associated event via     <code>Register&shy;Wait&shy;For&shy;Single&shy;Object<\/code>. \n<li>Thread pool thread goes idle. \n<li>Thread pool destroys the idle thread. \n<li>Due to thread affinity, this signals the handle. \n<li>The thread pool queues a task to process the handle that was signaled. \n<li>The task checks the registry key (observes that nothing changed)     and calls     <code>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/code> again. \n<li>Repeat. <\/ol>\n<p>Windows&nbsp;8 added a new flag to the <code>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/code> function: <code>REG_NOTIFY_THREAD_AGNOSTIC<\/code>. If you pass this flag, then the notification registration does not have thread affinity. If the thread that called <code>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/code> exits, the notification registration remains active. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>No longer tied to a specific thread.<\/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-44583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>No longer tied to a specific thread.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44583","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=44583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44583\/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=44583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}