2013-02-20 25 views
1

我創建了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(); 
     } 
    } 
} 
+0

也許我錯了,但如果表單崩潰並且應用程序繼續運行,那可能是因爲您只使用一個線程。嘗試使用一個線程啓動timer1時。 – Andres 2013-02-20 05:13:41

回答

2

由於計時器滴答事件中的代碼導致應用程序崩潰。應用程序即使在崩潰後也會繼續運行,因爲定時器未被禁用或處置。當您設置timer.Enabled = true時,Timer類請求GC不使用 - GCHandle.Alloc進行收集。所以即使在定時器對象引用無法訪問之後,它也不會被垃圾收集。解決您的計時器滴答事件中的問題,並正確放置計時器。

相關問題