[RESOLVED] problem the timer make the program to close itself-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] problem the timer make the program to close itself

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    4

    Resolved [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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,645

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    4

    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 02:57 PM.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,645

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    4

    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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,645

    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 06:14 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    4

    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 18th, 2017 at 12:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.