2012-04-06 19 views
0

私は、他にも色の変化のために画面上の5箇所をチェックするタイマーを持っています。私のプログラムは、電話システムのアプリケーションを監視し、5つのボタンからの新しい着信電話があるかどうかを確認します。私は投稿した別の質問に基づいて次のコードを使用しています。このコードを使用してMonitor an area of the screen for a certain color in Visual BasicVisual BasicでGetPixel/GetDCを使用したメモリリーク

Private Function CheckforCall() 
    Try 
     Dim queue1 As Integer = GetPixel(GetDC(0), 40, 573) 
     Dim queue2 As Integer = GetPixel(GetDC(0), 140, 573) 
     Dim queue3 As Integer = GetPixel(GetDC(0), 240, 573) 
     Dim queue4 As Integer = GetPixel(GetDC(0), 340, 573) 
     Dim queue5 As Integer = GetPixel(GetDC(0), 440, 573) 
     ReleaseDC(0) 

    <code snipped - Checks to see if the pixel color matches and 
     returns true or false> 

    Catch ex As Exception 
     Return False 
    End Try 
End Function 

、GDIは、非常に迅速かつ短期間内急騰オブジェクトのOutOfMemory例外がスローされます。私はDCを適切にリリースしていないと仮定していますが、それを行うための他の方法は見つけられません。

答えて

5
一度

コールGetDC(0)、それを変数に保存し、ReleaseDCに変数を渡す:

Dim hDC As IntPtr = GetDC(0) 
Try 
    Dim queue1 As Integer = GetPixel(hDC, 40, 573) 
    Dim queue2 As Integer = GetPixel(hDC, 140, 573) 
    Dim queue3 As Integer = GetPixel(hDC, 240, 573) 
    Dim queue4 As Integer = GetPixel(hDC, 340, 573) 
    Dim queue5 As Integer = GetPixel(hDC, 440, 573) 
    ... 
Catch ex As Exception 
    Return False 
Finally 
    ReleaseDC(0, hDC) 
End Try 

注意をReleaseDCが2つのIntPtr引数、hWndhDCを取ること。

+1

また、ReleaseDCをfinallyブロックに配置すると、例外が発生してもそれがクリーンアップされます。 –

+0

最後に、キャッチしません。これで通常の経路ではクリーンアップされません。 :-) –

+0

@JasonMalinowski:おっと!私は今それを手に入れましたか? –

関連する問題