{"id":103733,"date":"2020-05-07T07:00:00","date_gmt":"2020-05-07T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103733"},"modified":"2020-05-06T20:30:48","modified_gmt":"2020-05-07T03:30:48","slug":"20200507-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200507-00\/?p=103733","title":{"rendered":"Why does <CODE>Reg&shy;Notify&shy;Change&shy;Key&shy;Value<\/CODE> stop notifying once the key is deleted?"},"content":{"rendered":"<p>The <code>Reg\u00adNotify\u00adChange\u00adKey\u00adValue<\/code> function lets you when when a change occurs to a registry key. But once the key is deleted, the notifications stop. There is no notification if a new key is created with the same name. Why not?<\/p>\n<p>Well, because that&#8217;s a new key. It&#8217;s not the key that you passed to <code>Reg\u00adNotify\u00adChange\u00adKey\u00adValue<\/code>.<\/p>\n<p>The end of a registry key&#8217;s lifecycle is its deletion. Once it&#8217;s gone, it&#8217;s not coming back. Any handle to the registry key is now a zombie. A new registry key be created with the same name as the one that was deleted, but that&#8217;s a new key, which is not the same as the key that you were monitoring.<\/p>\n<p>It&#8217;s like saying, &#8220;You see that house at 123 Main Street? Let me know when ownership of that house changes.&#8221; When the house is sold, you get a notification.<\/p>\n<p>And then you get a notification that the house has been demolished.<\/p>\n<p>That&#8217;s the last notification you&#8217;ll get. If a new house is built at 123 Main Street, you won&#8217;t get any notifications about changes in ownership of that house, because that&#8217;s not the house that you subscribed to. Your subscription is for the house that was demolished, and it&#8217;s still demolished.<\/p>\n<p>(A more mundane example would be monitoring changes in a file. Once the file is deleted, its lifetime is over, and there won&#8217;t be any more notifications. If another file is created with the same name, well, it&#8217;s nice that it has the same name, but it&#8217;s not the same file. It&#8217;s a brand new file that happens to have the same name as the old one.)<\/p>\n<p>So what can you do if you want to monitor the new key, too?<\/p>\n<p>Open the new key and call <code>Reg\u00adNotify\u00adChange\u00adKey\u00adValue<\/code> to start monitoring it.<\/p>\n<p>If the new key doesn&#8217;t exist yet, you could create it. If you don&#8217;t want to create it, but you want to be notified when it is created, then you&#8217;ll have to set a notification on its parent (or closest ancestor that exists), watching for creation of subkeys. (You don&#8217;t need to watch recursively. You&#8217;ll get notified with the direct child key is created, at which point you can find the closest extent ancestor and repeat.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because that&#8217;s the last thing a key ever does.<\/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-103733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because that&#8217;s the last thing a key ever does.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103733","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=103733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103733\/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=103733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}