C# 为什么这个简单的移动表单在使用播放器时没有关闭

C# 为什么这个简单的移动表单在使用播放器时没有关闭,c#,mobile,windows-mobile,C#,Mobile,Windows Mobile,我用关闭按钮创建了这个简单的示例表单 不使用Interop.WMPLib.dll时,一切都正常工作 我见过其他应用程序使用它时没有出现问题,但为什么在我添加行时表单进程没有关闭: SoundPlayer myPlayer = new SoundPlayer(); 当然还有: if (myPlayer != null) { myPlayer.Dispose(); myPlayer = null;

我用关闭按钮创建了这个简单的示例表单

不使用Interop.WMPLib.dll时,一切都正常工作

我见过其他应用程序使用它时没有出现问题,但为什么在我添加行时表单进程没有关闭:

SoundPlayer myPlayer = new SoundPlayer();
当然还有:

if (myPlayer != null)
            {
                myPlayer.Dispose();
                myPlayer = null;
            }
表单关闭,但调试器VS2008仍处于活动状态。表单项目和dll仍处于活动状态

如果你给我发电子邮件xdasleepsense@gmail.com,我可以将压缩后的项目发送给您

下面是dll的类:

使用制度; 使用System.Collections.Generic; 使用系统文本; 使用系统线程; 使用System.Runtime.InteropServices; 使用WMPLib

命名空间WindowsMobile.Utilities { 公共代理无效SoundPlayerState已更改(SoundPlayerSender、SoundPlayerState newState)


我刚刚从这个链接中发现: 一个提示

因此,我添加了一个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");**

    } 
单击“确定”后,调试器也会认为它已完成。当然,我不能让用户单击“确定”


那么这里发生了什么呢?

一个消息框或下面的消息框解决了它。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");
    }
public void Dispose()
{
尝试
{
停止();
}
捕获(例外)
{
}
//需要这个,否则进程将无法退出?!
尝试
{
int ret=Marshal.FinalReleaseComObject(myPlayer);
}
捕获(例外)
{
}
myPlayer=null;
GC.Collect();
//如果你不这样做,它不会退出
//http://www.eggheadcafe.com/software/aspnet/31363254/media-player-freezing-app.aspx
对于(int s=0;s<100;s++)
{
Application.DoEvents();
睡眠(1);
}
GC.WaitForPendingFinalizers();
//MessageBox.Show(“应用程序退出”);
}

这对我来说并不完美(不得不猜测s的迭代次数——对我来说接近1000-10000次——特别可怕),但多亏了你的建议,至少我的应用不再崩溃。其他人能解释一下如何修复内存泄漏吗?附:这个问题真的缺少windows mobile标签
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");
    }