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

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

不使用Interop.WMPLib.dll时,一切都按预期工作

我已经看到其他应用程序使用它没有问题,但为什么不是我刚刚添加行时窗体进程关闭:

SoundPlayer myPlayer = new SoundPlayer(); 

并且当然处理它:

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

表单关闭但调试器VS2008仍处于活动状态。 表单项目和DLL仍处于活动状态。

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

下面是dll的类:

使用系统; 使用System.Collections.Generic; 使用System.Text; 使用System.Threading; 使用System.Runtime.InteropServices; 使用WMPLib;

namespace WindowsMo​​bile.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 } } 

MessageBox或Below解决了它。 谢谢。

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

我刚从这个链接中找到: http : //software.itags.org/pocketpc-developer/163455/一个提示……

所以我添加了一个消息框:

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

一旦我点击OK,调试器也认为它已经完成了。 当然我不能让用户点击OK。

那么这里发生了什么?