-
Apr 16th, 2017, 12:03 PM
#1
Thread Starter
New Member
[RESOLVED] problem the timer make the program to close itself
I have a tiny code that extract the pixel color at 1 second and play a sound when detect the color code that i set it. Everything work fine but after 2 hours the program is closing without any error or else. The initial code was setted at 100 (0.1 sec) and after 20 minutes the same shutdown.
I think is the buffer problem because the timer command are executed each time every second but i dont know how to trick this situation because vb6 doesnt have cache or else.
Code:
Option Explicit
Private Const SND_APPLICATION = &H80
Private Const SND_ALIAS = &H10000
Private Const SND_ALIAS_ID = &H110000
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_MEMORY = &H4
Private Const SND_NODEFAULT = &H2
Private Const SND_NOSTOP = &H10
Private Const SND_NOWAIT = &H2000
Private Const SND_PURGE = &H40
Private Const SND_RESOURCE = &H40004
Private Const SND_SYNC = &H0
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Command2_Click()
Form2.Show
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
End Sub
Private Sub Timer1_Timer()
Dim tPOS As POINTAPI
Dim sTmp As String
Dim lColor As Long
Dim lDC As Long
lDC = GetWindowDC(0)
Call GetCursorPos(tPOS)
lColor = GetPixel(lDC, Text1.Text, Text2.Text)
Label2.BackColor = lColor
Label5.Caption = tPOS.x
Label6.Caption = tPOS.y
sTmp = Right$("000000" & Hex(lColor), 6)
Caption = "Hello"
Label3.Caption = lColor
If Label3.Caption = Text4.Text Then
PlaySound Text3.Text, ByVal 0&, SND_FILENAME Or SND_ASYNC
Else
Label4.Caption = "nothing"
End If
End Sub
-
Apr 16th, 2017, 12:11 PM
#2
Re: problem the timer make the program to close itself
You have a memory leak which may be causing part of the problem. In my signature below, there is a tutorial on how to check for leaks. In your specific case, Section IV of that tutorial applies.
-
Apr 16th, 2017, 12:21 PM
#3
Thread Starter
New Member
Re: problem the timer make the program to close itself
Ok i think an object invoked need's to be uninvoked to avoid memory leaks from your tutorial. I wil try and tell you the result. Thank you.
Edited:
i Haved changed the 1DC to hWnd
i inserted after timer end if ReleaseDC hWnd, hDC and on top the declaration for ReleaseDC what to destroy next?
Last edited by danadra; Apr 16th, 2017 at 01:57 PM.
-
Apr 16th, 2017, 02:12 PM
#4
Re: problem the timer make the program to close itself
That was it. You were not releasing the return result of GetWindowDC().
Per MSDN: ... the ReleaseDC function must be called to release the device context. Not releasing the window device context has serious effects on painting requested by applications.
Changing the variable name from lDC to hWnd is your choice, though lDC is a better description of what the variable references; after all it is a device context, not a window handle. I would personally place the ReleaseDC call immediately after the GetPixel() call. The DC is no longer needed after that. The change you made should stop crashing your app unless the PlaySound API is causing you any problems.
-
Apr 16th, 2017, 05:00 PM
#5
Thread Starter
New Member
Re: problem the timer make the program to close itself
Still not working here is the update code:
Code:
Option Explicit
Private Const SND_APPLICATION = &H80
Private Const SND_ALIAS = &H10000
Private Const SND_ALIAS_ID = &H110000
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_MEMORY = &H4
Private Const SND_NODEFAULT = &H2
Private Const SND_NOSTOP = &H10
Private Const SND_NOWAIT = &H2000
Private Const SND_PURGE = &H40
Private Const SND_RESOURCE = &H40004
Private Const SND_SYNC = &H0
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Command2_Click()
Form2.Show
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim tPOS As POINTAPI
Dim sTmp As String
Dim lColor As Long
Dim hWnd As Long
hWnd = GetWindowDC(0)
Call GetCursorPos(tPOS)
lColor = GetPixel(hWnd, Text1.Text, Text2.Text)
Label2.BackColor = lColor
Label5.Caption = tPOS.x
Label6.Caption = tPOS.y
sTmp = Right$("000000" & Hex(lColor), 6)
Caption = "Hello"
Label3.Caption = lColor
If Label3.Caption = Text4.Text Then
PlaySound Text3.Text, ByVal 0&, SND_FILENAME Or SND_ASYNC
Else
Label4.Caption = "nothing"
End If
ReleaseDC hWnd, hDC
End Sub
Note* when the program close itself play a short 1 second sound from .wav sound file
-
Apr 16th, 2017, 05:08 PM
#6
Re: problem the timer make the program to close itself
Suggest these changes:
Dim hWnd As Long change to Dim lDC As Long
hWnd = GetWindowDC(0) change to lDC = GetWindowDC(0)
ReleaseDC hWnd, hDC change to ReleaseDC 0, lDC
I personally don't like using hWnd or hDC as variable names on forms. Why? Because hWnd and hDC are form property names and can cause confusion. In your last line of code you are using hDC. Since you did not declare hDC as a variable in your timer routine, that hDC is the form's hDC which is wrong. Also wrong is passing the first parameter of ReleaseDC the hWnd variable. You set the return value of GetWindowDC() to hWnd & that makes hWnd a pointer to a DC not a window handle. The first parameter of ReleaseDC() is a window handle and the second parameter is the DC.
It is also a good habit to release/destroy items when no longer needed, not later. So, I'd suggest moving the ReleaseDC() call to right after the GetPixel() call
Last edited by LaVolpe; Apr 16th, 2017 at 05:14 PM.
-
Apr 16th, 2017, 09:39 PM
#7
Thread Starter
New Member
Re: problem the timer make the program to close itself
5 hours and the program still running LaVolpe solution worked well. Thank's.
Last edited by danadra; Apr 17th, 2017 at 11:17 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|