使用 Interop.WMPLib.dll 在 Windows Mobile 6.x 上播放 mp3 文件,但我无法让 GC 干净地自行处理。对于长时间运行的应用程序(一小时或更长时间),我需要每 5 分钟播放一个短的 mp3(20-30 秒),所以我不能让 GC 正确处理 lib。@ajhvdb在 SO(Why is this simple Mobile Form not closed when using the player)上讨论了一个解决方案,但这对我来说不是一个足够好的解决方案,因为计时器黑客并不一致(我有时需要使用 10,000 个计时或者更多)。
有人可以推荐一种更好的处理 Dispose() 的方法,或者只是另一种可以让我在 Windows Mobile 6.x 上播放 mp3 文件的方法吗?
我目前拥有的(感谢@ajhvdb)是:
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");
}