2013-02-20 3 views
1

a video으로 내 문제를 설명합니다. 텍스트 형식 - 기본 폼이 손상된 것처럼 보일지라도 타이머가 실행되고 응용 프로그램이 계속 실행되는 이유가 확실하지 않으면 My Main 폼이 충돌합니다.양식이 응답하지 않지만 타이머가 계속 실행 중입니다.

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

답변

2

타이머 틱 이벤트의 코드로 인해 응용 프로그램이 충돌합니다. 타이머가 작동하지 않거나 처리되지 않기 때문에 응용 프로그램이 충돌 한 후에도 계속 실행됩니다. Timer 클래스는 timer.Enabled = true로 설정할 때 GCHandle.Alloc을 사용하여 GC가 수집하지 않도록 요청합니다. 따라서 타이머 객체 참조가 도달 할 수 없게 된 후에도 가비지 수집되지 않습니다. 타이머 틱 이벤트의 문제를 해결하고 타이머를 적절히 조정하십시오.

관련 문제