我使用关闭按钮创建了这个简单的示例表单。
当不使用Interop.WMPLib.dll
时,一切都按预期工作我已经看到其他应用程序使用它没有问题,但为什么在我添加行时不关闭Form进程:
SoundPlayer myPlayer = new SoundPlayer();
当然处理它:
if (myPlayer != null)
{
myPlayer.Dispose();
myPlayer = null;
}
表单关闭但调试器VS2008仍处于活动状态。表单项目和DLL仍处于活动状态。
如果您发送电子邮件至xdasleepsense@gmail.com,我可以将压缩项目发送给您。
以下是dll的类:
使用System; 使用System.Collections.Generic; 使用System.Text; 使用System.Threading; 使用System.Runtime.InteropServices; 使用WMPLib;
命名空间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
}
}
答案 0 :(得分:1)
MessageBox或Below解决了它。 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");
}
答案 1 :(得分:0)
我刚从此链接中找到: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。
那么这里发生了什么?