为什么在使用播放器时未关闭此简单的移动表单 [英] Why is this simple Mobile Form not closed when using the player
问题描述
我用关闭按钮创建了这个简单的示例表单.
I created this simple sample Form with the close button.
当不使用Interop.WMPLib.dll时,一切都按预期工作
Everything is working as expected when NOT using the Interop.WMPLib.dll
我已经看到其他应用程序使用它没有问题,但是为什么我添加行时Form进程没有关闭:
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();
当然要处理它:
if (myPlayer != null)
{
myPlayer.Dispose();
myPlayer = null;
}
窗体关闭,但调试器VS2008仍处于活动状态. Form项目和dll仍处于活动状态.
The Form closes but the debugger VS2008 is still active. The Form project and the dll are still active.
如果您给我发送电子邮件到xdasleepsense@gmail.com,我可以给您发送压缩的项目.
If you send me an email to xdasleepsense@gmail.com, I can send you the zipped project.
以下是dll的类:
使用系统; 使用System.Collections.Generic; 使用System.Text; 使用System.Threading; 使用System.Runtime.InteropServices; 使用WMPLib;
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Runtime.InteropServices; using WMPLib;
命名空间WindowsMobile.Utilities { 公共委托void SoundPlayerStateChanged(SoundPlayer sender,SoundPlayerState newState);
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
}
}
推荐答案
一个MessageBox或更低版本解决了它.谢谢.
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");
}
这篇关于为什么在使用播放器时未关闭此简单的移动表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!