Wednesday, June 23, 2010

How To: Dispose a Module in PRISM

There have been several questions about disposing Module in Prism at CodePlex. Moreover, several workarounds have been proposed.
The fastest workaround was to extend the CloseView() method at the Presenter (of the View we want to dispose) as follows:
Public void OnCloseView()
{

if (View is IDisposable)
{
((IDisposable)View).Dispose();
//Disposes the view.
}
}
We need to implement IDisposable interface in all the controls so that when we close the view we can able to free up all the resource from the module which will avoid memory leak. Since the freeing up of resources is not done automatically when we close a view .In situation like when the module uses the Dispatcher Timer and if the timer is not closed say for instance, then we can free up the Dispatcher Timer using the IDisposable.

Please, remember that you must consider when and where to dispose (if you don’t want to dispose at every time). Besides, if you place this handler in the first position (regarding handlers order), you won’t be able to cancel the disposal, so I recommend using this carefully and, of course, consciously!
- Cheers, ;)

0 comments:

Post a Comment