1

私の問題を説明するビデオを作成しました。テキスト フォーム - タイマーの実行時にメイン フォームがクラッシュし、メイン フォームがクラッシュしたように見えても、アプリケーションが実行され続ける理由がわかりません。

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

1 に答える 1

2

タイマー ティック イベントのコードが原因で、アプリケーションがクラッシュします。タイマーが無効化または破棄されていないため、アプリがクラッシュした後も引き続き実行されます。timer クラスは、timer.Enabled = true を設定すると、GCHandle.Alloc を使用して収集しないように GC に要求します。そのため、タイマー オブジェクト参照が到達不能になった後でも、ガベージ コレクションは行われません。タイマー ティック イベントの問題を修正し、タイマーを適切に破棄します。

于 2013-02-20T05:47:17.840 に答える