【问题标题】:Form is not responding but timer is still running表单没有响应,但计时器仍在运行
【发布时间】:2013-02-05 00:42:54
【问题描述】:

我创建了a video,它解释了我的问题。以文本形式 - 我的主窗体在计时器运行时崩溃,我不确定为什么,应用程序继续运行,即使主窗体似乎已经崩溃。

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();
        }
    }
}

【问题讨论】:

  • 也许我错了,但是如果表单崩溃并且应用程序继续运行,那可能是因为您只使用了一个线程。启动 timer1 时尝试使用一个线程。

标签: c# winforms audio windows-7


【解决方案1】:

由于您的代码在计时器滴答事件中,应用程序崩溃。即使在崩溃后应用程序仍会继续运行,因为计时器未被禁用或处置。当您设置 timer.Enabled = true 时,Timer 类使用 - GCHandle.Alloc 请求 GC 不收集。因此,即使在计时器对象引用不可访问之后,它也不会被垃圾收集。修复计时器滴答事件中的问题并正确处理计时器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2022-10-20
    • 2019-02-04
    相关资源
    最近更新 更多