Why is this simple Mobile Form not closed when using the player

北城余情 提交于 2019-12-02 02:24:10

问题


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 xdasleepsense@gmail.com, 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
}
}

回答1:


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



回答2:


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?



来源:https://stackoverflow.com/questions/2700219/why-is-this-simple-mobile-form-not-closed-when-using-the-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!