Consulting

Results 1 to 8 of 8

Thread: Solved: Timer event = 100% CPU Usage

  1. #1

    Solved: Timer event = 100% CPU Usage

    I don't understand this at all, every time I use this timer procedure, it shoots up to 100% CPU Usage... even if I take pretty much EVERYTHING out of the code except the timer.

    [VBA]
    With Me
    Start = Timer
    Do While Timer < Start + CountDownTimer

    counter = Format(CountDownTimer - (Timer - Start), "#")
    DoEvents
    With .lblSecondsCounter
    If .Caption <> counter Then

    If .Caption = "10" Then
    .ForeColor = 255
    Me.btnCancelAutorun.ForeColor = 255
    Me.lblSeconds.ForeColor = 255
    End If
    pctcompleted = (Timer - Start) / CountDownTimer
    UpdateProgress (pctcompleted)
    .Caption = counter
    End If
    End With

    If CancelAutoRun = True Then
    RunAutomaticScript = False
    .btnGenerateTable.Visible = True
    .btnUpdateProdDB.Visible = True
    GoTo EndGame
    End If
    Loop
    End With
    RunAutomaticScript = True
    [/VBA]
    Notes:
    CancelAutoRun is set to true by a button click.
    UpdateProgress works with a progress bar. Disabling it does not effect CPU usage.
    Endgame just does some simple exit procedure-type stuff.

    Any advice would be appreciated... I'm just scratching my head here.

    David

  2. #2
    Oh: Access 2003. And this is called from a OnLoad form procedure. Form is non-modal.

  3. #3
    Feel stupid that I solved my own stupid problem. The sleep method solves this:

    [VBA]

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    [/VBA]

    Plus:

    adding [VBA]Sleep(100) [/VBA] into the loop. Causes the macro to pause execution, so as to avoid hogging all the resources for a loop that's effectively doing nothing.

  4. #4
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    An enhancement to this idea is to capture the start time of the loop and compare it to a threshold value to force a timeout. For example, if I'm automating the creation of files, and I need to move each created file to a specific directory to avoid overwriting, then I might measure the time I started my Do While to raise a custom error to avoid an infinite loop.

  5. #5
    That's true, but when you're only trying to wait for a user response for a specified amount of time, there's just a loop running over and over again. So even if I'm only waiting ten seconds (and doing nothing but waiting in a loop until either the user responds or the time has elapsed), if I don't use a sleep call, the CPU usage will shoot up to 100% because the VBA engine will hog all available resources to execute that empty loop. Or am I still missing something? (likely)

  6. #6
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Sorry, your situation got me off on a tangent. My comment was a general comment that applies more to this situation:

    [VBA] Do While Condition = False
    DoEvents
    Sleep 100
    Loop
    [/VBA]

    When you don't know that condition will always come true, but you want to give it some time (eg. waiting on some other process to output a file), you can add something like this:

    [VBA]
    datLoopStart = Now
    Do While Condition = False
    DoEvents
    Sleep 100
    if datediff("s",datLoopStart,Now)=30 then
    fLoopFailed = True
    Exit Do
    end if
    Loop
    [/VBA]

  7. #7
    Yep. I got you.

    My code could have been more clear (could have, for example, included the EndGame reference). Is there any advantage to placing the Condition as the Do While Loop as you have it, versus putting the Timer as the Do While Loop as I have it?

    Both check the condition and the time every loop cycle, right?

    Anyway, thanks for the input.

  8. #8
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    The example I suggested is looking for some other process to output a file, so in that case, the loop condition would be...

    [VBA]
    datLoopStart = Now
    Do While MyFileExists= False
    DoEvents
    Sleep 100
    If datediff("s",datLoopStart,Now)=30 Then
    fLoopFailed = True
    Exit Do
    End if
    Loop
    [/VBA]

Posting Permissions

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