Dispatcher stops object disposal

Nov 6, 2008 at 3:12 AM
Hi,

I have discovered an interesting piece of behaviour to do with the idea of using a dispatcher to manage each player object.

In a similar mediaelement that I have been building, was finding that a dialog containing one of my mediaelements was not being disposed when I closed the dialog. After a suitably frustrating period of profiling & debugging, I narrowed it down to the mediaplayer dispatcher - it inhibits the mediaplayer and the mediaelement objects (and the containing window) from being finalized because the dispatcher doesn't shut down until it is told to. The realisation dawned that this is basically the same as a window itself - it's possible to create subwindows each running in their own dispatcher, but you need to add the behaviour to shut down the dispatcher when you close the window.

I've considered a few ideas like traversing the visual tree to find the Window so we can hook the closing event and shut down each player dispatcher when the window is closed.... but it just doesn't seem right to do that. So I settled on adding a disposable implementation to both the mediaplayer and mediaelement (as you already do here), and a reminder to myself to call dispose on the mediaelement in the closing eventhandler of the dialog (in my case).

All I then really needed to add to the mediaplayer dispose method is a call to Dispatcher.BeginInvokeShutdown(), cleaning up the other elements like a backgroundworker I'm using to handle directshow events and the like as well.

This solves the problem, and I'm happy enough with it.... but I'm wondering if there is a better solution that would fit more seamlessly in this type of control? I'm 98% sure that exactly this same situation would come up in this project if you added a media control to a window & closed the window, I just don't have the facility to both build & profile this (in .net 3.5) myself.
Jul 19, 2009 at 4:05 PM

Ahhhh! Thanks, I've been looking for this!! ;)