2011-12-12 3 views
1

타이머가 틱 할 때마다 응용 프로그램이 추가 메모리를 계속 사용합니다.
GetActeWindowTitle() 및 window2에 핸들을 처리하는 방법은 무엇입니까?
이 용도로 메모리 사용을 어떻게 관리해야합니까?CPU 사용량 관리

내 코드 :

public partial class Window1 : Window 
{ 
    Window2 window2 ; 

    System.Windows.Forms.Timer timer1; 
    public Window1() 
    { 
     InitializeComponent(); 

     timer1 = new Timer(); 
     timer1.Interval = 900000; 
     timer1.Tick += new EventHandler(timer1Tick); 
     timer1.Start();    
    } 

    private void timer1Tick(object Sender, EventArgs e) 
    {        
     window2 = new window2Window(); 

     if (((GetActeWindowTitle().IndexOf("Outlook") != -1) || 
     (GetActeWindowTitle().IndexOf("Word") != -1))) 
     { 
      window2.Close(); 
     } 
     else 
     { 
      window2.Show(); 
      window2.Topmost = true; 
     } 
    } 

    private string GetActeWindowTitle() 
    { 
     const int nChars = 256; 
     IntPtr handle = IntPtr.Zero; 
     StringBuilder Buff = new StringBuilder(nChars); 
     handle = GetForegroundWindow(); 
     if (GetWindowText(handle, Buff, nChars) > 0) 
     { 
      return Buff.ToString();    
     } 
     return null; 
    }  
} 
+0

CPU 사용량이나 메모리에 관한 질문입니까? 'window2Window' 란 무엇이고 IDisposable을 구현합니까? 가비지 수집 후 메모리 사용량을 어떻게 측정하고 있습니까? –

+0

왜 windows2를 작성한 다음 닫으십니까? 왜 새로운 윈도우 2가 아닌가? – Paparazzi

+0

제가 taskmanager를 볼 때, 메모리는 모든 타이머 틱마다 올라갑니다. –

답변

1

어쩌면을 IntPtr에서 참조하는 Interop를 호출에서 할당됩니다 메모리를 해제하여.

Marshal.FreeCoTaskMem(handle); 
+0

답장을 보내 주셔서 대단히 감사합니다. Marshal.FreeCoTaskMem (처리)을 사용했습니다. 메모리가 누출되었습니다. 15 분마다 100k를 의미합니다. –

0

이유는 단지 그것을 닫 새로운 window2Window를 만듭니다

당신이 GetActeWindowTitle()에서 처리 사용 후이 시도?

// window2 = new window2Window(); 

if (((GetActeWindowTitle().IndexOf("Outlook") != -1) || 
(GetActeWindowTitle().IndexOf("Word") != -1))) 
{ 
    // window2.Close(); 
} 
else 
{ 
    window2 = new window2Window(); 
    window2.Show(); 
    window2.Topmost = true; 
}