gpt4 book ai didi

c# - 为什么这个简单的Mobile Form在使用播放器时没有关闭

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:39 27 4
gpt4 key购买 nike

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

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

我已经看到其他应用程序使用它没有问题,但为什么当我只添加以下行时 Form 进程没有关闭:

SoundPlayer myPlayer = new SoundPlayer();

当然还有处置它:

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

窗体关闭但调试器 VS2008 仍处于事件状态。 Form 项目和 dll 仍处于事件状态。

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

下面是 dll 的类:

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

命名空间 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
}
}

最佳答案

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

关于c# - 为什么这个简单的Mobile Form在使用播放器时没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700219/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com