Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF control containing an IDisposable member

Tags:

c#

.net

wpf

I have a member in the WPF code behind that is disposable (meaning it implements the IDisposable interface)

I do not see any Dispose method I can override from UserControl in WPF so I can dispose of the member in my wpf usercontrol

What is the correct way to dispose of a member in a WPF usercontrol?

It's a usercontrol that wraps a private member which implements the IDisposable interface. Thus I need to dispose of that member somewhere. In the tradition winform, the usercontrol had a Dispose method that could be overriden so that in the override I could dispose of the private member. But in the WPF usercontrol, there is no such thing. So I was wondering where can I dispose of the private member in the wpf usercontrol.

My question is not about disposing the usercontrol, but where to dispose one of its private member which implement the IDisposable interface

like image 600
pdiddy Avatar asked Sep 10 '10 14:09

pdiddy


2 Answers

You could use the Unloaded event of the UserControl to do your resource cleanup.

like image 189
MrDosu Avatar answered Oct 20 '22 23:10

MrDosu


You would do it in the Dispatcher.ShutDownStarted event handler. See this question for details (which in turn refers to this blog post).

like image 1
Mike Post Avatar answered Oct 21 '22 01:10

Mike Post