{"id":4783,"date":"2014-03-30T15:21:08","date_gmt":"2014-03-30T15:21:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/disposing-wpf-user-controls-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:21:08","modified_gmt":"2014-03-30T15:21:08","slug":"disposing-wpf-user-controls-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/disposing-wpf-user-controls-collection-of-common-programming-errors\/","title":{"rendered":"Disposing WPF User Controls-Collection of common programming errors"},"content":{"rendered":"<p>I have created a custom WPF user control which is intended to be used by a third party. My control has a private member which is disposable, and I would like to ensure that its dispose method will always get called once the containing window\/application is closed. However, UserControl is not disposable. I tried implementing the IDisposable interface and subscribing to the Unloaded event but neither get called when the host application closes. If at all possible, I don&#8217;t want to rely on consumers of my control remembering to call a specific Dispose method.<\/p>\n<pre><code> public partial class MyWpfControl : UserControl\n {\n     SomeDisposableObject x;\n\n     \/\/ where does this code go?\n     void Somewhere() \n     {\n         if (x != null)\n         {\n             x.Dispose();\n             x = null;\n         }\n\n     }\n }\n<\/code><\/pre>\n<p>The only solution I have found so far is to subscribe to the Dispatcher&#8217;s ShutdownStarted event. Is this a reasonable approach?<\/p>\n<pre><code>this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have created a custom WPF user control which is intended to be used by a third party. My control has a private member which is disposable, and I would like to ensure that its dispose method will always get called once the containing window\/application is closed. However, UserControl is not disposable. I tried implementing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4783","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4783"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4783\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}