views:

120

answers:

1

Using the Interop.WMPLib.dll to play mp3 files on Windows Mobile 6.x, but I can't get the GC to cleanly dispose of itself. I need to play a short mp3 (20-30 seconds) every 5 minutes for a long-running app (one hour or more), so I cannot afford to not have the GC dispose of the lib correctly. One solution was discussed on SO (http://stackoverflow.com/questions/2700219/why-is-this-simple-mobile-form-not-closed-when-using-the-player/2702086#2702086) by @ajhvdb but it is not a good enough solution for me, because the timer hack is not consistent (I needed to sometimes use timings of 10,000 or more).

Can someone recommend a better way of handling Dispose() or just another way I can get mp3 files playing with Windows Mobile 6.x?

What I currently have (thanks to @ajhvdb) is:

public void Dispose()
    {
        try
        {
            Stop();
        }
        catch (Exception)
        {
        }
        // need this otherwise the process won't exit?!
        try
        {
            int ret = Marshal.FinalReleaseComObject(myPlayer);
        }
        catch (Exception)
        {
        }
        myPlayer = null;
        GC.Collect();

        //If you don't do this, it will not quit
        //http://www.eggheadcafe.com/software/aspnet/31363254/media-player-freezing-app.aspx
        for (int s = 0; s < 100; s++)
        {
            Application.DoEvents();
            Thread.Sleep(1);
        }
        GC.WaitForPendingFinalizers();

        //MessageBox.Show("Application Exiting");
    }
A: 

Are you using the AxHost stuff from the MSDN article? If so, there's a bug in it that causes objects to not get cleanly destroyed.

ctacke