This project has moved and is read-only. For the latest updates, please go here.

Best Place to Dispose Messages

May 26, 2011 at 2:48 PM

I have some messages that have code in them which will prevent the GC from sweeping them away unless some clean up is done. If I implement IDisposable on my message, where is the best place in Messenger to call Dispose? My best guess is in Messenger.CleanUpList but I am not really sure where. Any help is appreciated.




Jun 14, 2011 at 9:26 AM

From my experience it is best to cleanup message handlers as soon as they are no longer needed.

For a view you could register the handler in the loaded event (or the constructor) and call Messenger.Unregister(view) in the Unloaded event of the view.

In case of a ViewModel you can call Cleanup once the message handling in the ViewModel is no longer needed, or you could implement IDisposable and call Cleanup from within. Side Note: Up to version 3 there was a implementation of IDispose on the ViewModelBase that did just that; but as of version 4 you will have to implement the IDisposabele interface yourself.