Using Timer to Reduce Flicker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OldBirdman
    Contributor
    • Mar 2007
    • 675

    #1

    Using Timer to Reduce Flicker

    I am comparing picture names in a Folder with the names in a table, in order to find files that are not referenced within the database. I call these "Orphans"

    This routine is imbedded in a large program, and I am only presenting a small fraction of the code. All variables are declared. Those beginning with str... are String, beginning with ii... are Long, lbl... are Labels, etc.

    The routine worked, but the image flickered because line 22 of the code is executed ~80K times. To reduce the flicker, I inserted the lines marked as "Added line". The code still works, but the label on the form (lblDisplay) isn't displayed until cmdFindOrphans_ Click exits. As a matter of fact, the timer event appears not to be triggered, as the function (code lines 28-30) is never executed.

    A toggle breakpoint at line 25 has TimerInterval = 250, as it should. Before you ask, yes, the property sheet for the form has [Event Procedure], and the [. . .] gets me to the code corresponding to line 28.

    Is there something I am missing here?


    Code:
    Private Sub Form_Load()
        . . . . 
        Me.TimerInterval = 0 'Added line
    End Sub 
    
    cmdFindOrphans_Click
        iiRecCt = 0
        iiOrphanCt = 0
        lblDisplay.Caption = ""
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("SELECT . . .")
    
        strFile = Dir(strPath & "\*.*")
        Me.TimerInterval = 250 'Added line
        Do Until strFile = ""
            iiRecCt = iiRecCt + 1
            If IsOrphan(arg1, . . .) = True Then
                iiOrphanCt = iiOrphanCt + 1
                . . . .
            End If
            lblDisplay.Caption = "Checked=" & iiRecCt & "   Orphans=" & iiOrphanCt
            'Me.Repaint 'Original line
            strFile = Dir()
        Loop
        Me.TimerInterval = 0 'Added line
    End Sub
    
    Private Sub Form_Timer() 'Added line
        Me.Repaint 'Added line
    End Sub 'Added line
    OldBirdman
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I've frequently seen the use of the Timer given as the cause of screen flicker, but I've never heard of it being used to suppress screen flicker! The problem here, I expect, for the label not appearing, is that you're repainting the screen too frequently. Timer Interval = 1000/second, so an Interval of 250 means the screens repainting every 1/4 of a second.

    Linq ;0)>

    Comment

    • OldBirdman
      Contributor
      • Mar 2007
      • 675

      #3
      I could have no label, and no counters. The problem would go away. I would have to substitute the 'hourglass' for the cursor, to show I was actually doing something, and not just looping or idle.

      I had flicker when I did not have the timer. I checked 4,000 files in about 9 seconds, so the repaint rate would have been about 400/second, somewhat faster than 4/second.

      I could live with the flicker, but I felt that the repaint might be quite expensive, and reducing the number of times by a factor of 100 might speed it up.

      OldBirdman

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Hi, OldBirdman.

        To the best of my knowledge events (including that from form timer) are not asynchronous in VBA. No timer event will be fired while code is running.
        You may try to use win timer but this maybe a bit unsafe.

        Regards,
        Fish

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by FishVal
          Hi, OldBirdman.

          To the best of my knowledge events (including that from form timer) are not asynchronous in VBA. No timer event will be fired while code is running.
          You may try to use win timer but this maybe a bit unsafe.

          Regards,
          Fish
          I do believe that FishVal is right on this one, as long as code execution is contained within the Do...Loop, the Timer() Event will never fire.

          Comment

          • OldBirdman
            Contributor
            • Mar 2007
            • 675

            #6
            Thank you! Not sure what "asynchrono us" is, but that's OK.

            Within my loop, I inserted an iiCounter and incremented for each iteration of the loop. Then:
            Code:
            . . . 
            If iiCounter mod 1000 = 0 then 
                lblStatus.Caption = "Checked=" & iiCounter & " --- Found=" & iiFound 'Checked=3000 --- Found=8'
                Me.Repaint 
            End If
            . . .
            I can adjust the speed of the display update by tinkering with the 'mod' value. I consider this problem solved. Thank you all again.

            OldBirdman

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Not a problem.
              Good luck.

              Comment

              Working...