C# 窗体没有响应,但计时器仍在运行
我已经创造了一个解释我问题的模型。在文本表单中-当计时器运行时,我的主表单崩溃,我不知道为什么,应用程序一直在运行,即使主表单似乎崩溃了C# 窗体没有响应,但计时器仍在运行,c#,winforms,audio,windows-7,C#,Winforms,Audio,Windows 7,我已经创造了一个解释我问题的模型。在文本表单中-当计时器运行时,我的主表单崩溃,我不知道为什么,应用程序一直在运行,即使主表单似乎崩溃了 namespace ItunesGamesEqualiser { public partial class GUI : Form { private void refreshBar_Scroll(object sender, EventArgs e) { timer1.Interval =
namespace ItunesGamesEqualiser
{
public partial class GUI : Form
{
private void refreshBar_Scroll(object sender, EventArgs e)
{
timer1.Interval = prbLevel.Value;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
AudioSessionControl session;
AudioSessionControl itunesSession;
MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
// Note the AudioSession manager did not have a method to enumerate all sessions in windows Vista
// this will only work on Win7 and newer.
for (int i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
{
itunesSession = device.AudioSessionManager.Sessions[i];
if (itunesSession.SessionIdentifier.Contains("iTunes") == true) //find itunes audio service
{
for (int j = 0; j < device.AudioSessionManager.Sessions.Count; j++)
{
session = device.AudioSessionManager.Sessions[j];
if (session.SessionIdentifier.Contains("iTunes") == false) //find game audio service
{
if (session.State == AudioSessionState.AudioSessionStateActive)
{
Process p = Process.GetProcessById((int)session.ProcessID);
Console.WriteLine("ProcessName: {0}", p.ProcessName);
AudioMeterInformation mi = session.AudioMeterInformation;
AudioMeterInformation imi = itunesSession.AudioMeterInformation;
SimpleAudioVolume vol = session.SimpleAudioVolume;
SimpleAudioVolume ivol = itunesSession.SimpleAudioVolume;
//int start = Console.CursorTop;
ivol.MasterVolume = 1;
float origVol = ivol.MasterVolume;
while (true)
{
//Draw a VU meter
int len = (int)(mi.MasterPeakValue * 79);
int ilen = (int)(imi.MasterPeakValue * 79);
//Console.SetCursorPosition(0, start);
//Game Meter
if (len > 30)
{
float curvol = origVol - (0.1f * (len - 10) / 10);
if (curvol < 0) curvol = 0;
ivol.MasterVolume = curvol;
prbLevel.Value = len;
}
else
{
ivol.MasterVolume = origVol;
//Console.WriteLine("null");
}
}
}
}
}
}
}
//If we end up here there where no open audio sessions to monitor.
lblName.Text = "No game found, please start game and iTunes";
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
}
名称空间ItunesGamesEqualiser
{
公共部分类GUI:表单
{
私有无效刷新条\滚动条(对象发送方,事件参数e)
{
timer1.Interval=prbLevel.Value;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
timer1.Start();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
音频会话控制会话;
音频会话控制环境;
MMDeviceEnumerator DevEnum=新的MMDeviceEnumerator();
MMDevice device=DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender、ERole.eMultimedia);
//注意AudioSession管理器没有枚举windows Vista中所有会话的方法
//这仅适用于Win7及更高版本。
对于(int i=0;i30)
{
浮动曲线=初始值-(0.1f*(透镜-10)/10);
如果(curvol<0)curvol=0;
ivol.MasterVolume=曲线音量;
prbLevel.Value=len;
}
其他的
{
ivol.MasterVolume=原始音量;
//控制台。写入线(“空”);
}
}
}
}
}
}
}
//如果我们在这里结束,那里没有开放的音频会话可以监视。
lblName.Text=“未找到游戏,请启动游戏和iTunes”;
}
私有void btnStop_单击(对象发送方,事件参数e)
{
timer1.Stop();
}
}
}
应用程序崩溃,因为您的代码出现在计时器滴答声事件中。应用程序即使在崩溃后仍继续运行,因为计时器未被禁用或释放。当您设置Timer.Enabled=true时,Timer类使用-GCHandle.Alloc请求GC不收集。因此,即使在计时器对象引用不可访问之后,它也不会被垃圾收集。修复计时器滴答事件中的问题,并正确使用计时器。可能我错了,但如果窗体崩溃,应用程序继续运行,可能是因为您只使用了一个线程。尝试在启动计时器1时使用一个线程。