views:

99

answers:

2

Hi,

I created this simple sample Form with the close button.

Everything is working as expected when NOT using the Interop.WMPLib.dll

I've seen other applications using this without problems but why isn't the Form process closed when I just add the line:

SoundPlayer myPlayer = new SoundPlayer();

and of course dispose it:

if (myPlayer != null)
            {
                myPlayer.Dispose();
                myPlayer = null;
            }

The Form closes but the debugger VS2008 is still active. The Form project and the dll are still active.

If you send me an email to [email protected], I can send you the zipped project.

Below is the class for the dll:

using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Runtime.InteropServices; using WMPLib;

namespace WindowsMobile.Utilities { public delegate void SoundPlayerStateChanged(SoundPlayer sender, SoundPlayerState newState);

public enum SoundPlayerState
{
    Stopped,
    Playing,
    Paused,
}


public class SoundPlayer : IDisposable
{
    [DllImport("coredll")]
    public extern static int waveOutSetVolume(int hwo, uint dwVolume);

    [DllImport("coredll")]
    public extern static int waveOutGetVolume(int hwo, out uint dwVolume);

    WindowsMediaPlayer myPlayer = new WindowsMediaPlayer();

    public SoundPlayer()
    {
        myPlayer.uiMode = "invisible";
        myPlayer.settings.volume = 100;
    }

    string mySoundLocation = string.Empty;

    public string SoundLocation
    {
        get { return mySoundLocation; }
        set { mySoundLocation = value; }
    }

    public void Pause()
    {
        myPlayer.controls.pause();
    }

    public void PlayLooping()
    {
        Stop();
        myPlayer.URL = mySoundLocation;
        myPlayer.settings.setMode("loop", true);
    }

    public int Volume
    {
        get { return myPlayer.settings.volume; }
        set { myPlayer.settings.volume = value; }
    }

    public void Play()
    {
        Stop();
        myPlayer.URL = mySoundLocation;
        myPlayer.controls.play();
    }

    public void Stop()
    {
        myPlayer.controls.stop();
        myPlayer.close();
    }

    #region IDisposable Members

    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();
    }

    #endregion
}

}

A: 

I just found from this link: http://software.itags.org/pocketpc-developer/163455/ a hint...

So I added a messagebox:

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(); 

        **MessageBox.Show("Application Exiting");**

    } 

once I click OK, the debugger also thinks it's finished. Of course I can't have the user click OK.

So what's happening here?

+1  A: 

A MessageBox or Below solved it. Thx.

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");
    }
How can I set this to solved?
This doesn't work perfectly for me (having to guess the s iterations - for me it's closer to 1000-10,000 - is particularly frightening), but thanks to your suggestions, at least my app doesn't crash any longer. Can someone else shed some light on how to fix the memory leak?
pithyless
PS. this question is really missing the windows-mobile tag
pithyless