Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to call Dispose() method in WPF application

Tags:

c#

wpf

dispose

I have a simple WPF single window application contains Textboxes and Buttons. And I also use NotifyIcon and DateTimePicker of Windows Forms in WPF window. How can I effectively dispose all the controls?

like image 254
Sauron Avatar asked Aug 07 '09 06:08

Sauron


2 Answers

Hardly anything in WPF has a Dispose method. The vast majority of classes encapsulate purely managed information. You can attach an object into the tree (e.g. via a Children.Add method) and you can remove it again - that's how the state management works. It exactly doesn't fit into the IDisposable pattern, because once you've removed a control you can add it again, whereas Dispose means forever (although you could use Dispose to manage it in addition to Add/Remove methods).

A discussion about it on the Microsoft forums.

There are a few things that ought to be IDisposable but aren't, like DispatcherTimer, and there's nothing to stop you from implementing IDisposable on your own classes. It's up to you when to call Dispose; basically when you know you aren't going to be using the object any more.

For a Window you just call Close to close it, and WPF takes care of everything else.

like image 60
Daniel Earwicker Avatar answered Sep 21 '22 21:09

Daniel Earwicker


I would say that the same rule applies in WPF applications as in any other .NET applications: if an object implements IDisposable, you should call Dispose when you are done using it. If you dynamically load and unload controls, and they do not implement IDisposable, just setting any references to null (and detaching any event handlers) should be enough for the garbage collector to do its job.

like image 31
Fredrik Mörk Avatar answered Sep 22 '22 21:09

Fredrik Mörk