Solved

Sleep functon from kernel Library works inconsistent in VBA

Posted on 2008-10-10
4
1,312 Views
Last Modified: 2013-11-27
I have a program that uses the sleep function in VBA from the kernel library.

It has a tendency to work inconsistently.  All of the sudden it will start to run really slowly.  I will have to turn the time down from 1 to .1 in order to get it to work at the speed it used to.  

There are not any other programs running that would "slow" it down.  I am running the application on a core 2 processor.

Is there any other functions I can use to delay something by a millesecond?  Perhaps something that may act more consistent.

Thanks,
JOe K.
Public timerCounter As Integer
Const TIMERLIMIT = 1
Private Declare Sub sapiSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
 
Sub sSleep(lngMilliSec As Long)
    If lngMilliSec > 0 Then
        Call sapiSleep(lngMilliSec)
    End If
End Sub

Open in new window

0
Comment
Question by:ClaudeWalker
  • 2
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 22688841
i am just using

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

and to delay i just use

sleep 500  ' or any other value
0
 
LVL 57
ID: 22688897
 
  Your not not going to get that fine of control outside of running in the OS kernel.  A thread shift alone can easily takes 15ms or more.  You'd have to be running in a process that was no swapable and with a high priority.

  I'm curious; what are you trying to do that takes such fine control?

JimD.
0
 

Author Comment

by:ClaudeWalker
ID: 22689059
Using the Alias and calling the other sub was the slowing down issue as far as I can tell. Because the problem is intermittent only time will tell.

Here is the code I am using sleep for.

I created a "screen saver" to prevent burnin on our IO board screen.  Runs every 30 minutes and it cycles from red to blue to green to yellow to blue to red again.  It takes about 30 seconds  Then displays whose gone and whose in the office.  



If timerCounter = TIMERLIMIT Then
        For v = 1 To 36
            For Each ctl In Me.Controls
                If ctl.ControlType = acLabel Then
                    numCtls = numCtls + 1
                    If v = 1 Then
                        ctl.Caption = ""
                        backCol = 10001000
                        ctl.BorderStyle = 0
                    End If
                    
                    If v = 36 Then
                        ctl.BorderStyle = 1
                    End If
                    
                    ctl.BackColor = backCol
                End If
                
                If v < 19 Then
                    backCol = backCol + 20
                Else
                    backCol = backCol - 20
                End If
                                
                Me.Repaint
                Sleep 3
            Next ctl
        Next v
        
        timerCounter = 0
    End If

Open in new window

0
 

Author Comment

by:ClaudeWalker
ID: 22689520
I moved it to a slower computer and sleep didn't work as well.  It seemed to round up after .5 for the number of milleseconds.  I .5 was close but I needed more like .7 and it went as slow as 1.

I made a ghetto solution.

For ghettoTimer = 1 to 150000

next ghettoTimer

It gave much more control and seemed to work well.

Thanks,
JOe K.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Programmer 14 49
Access VBA, adding Progress Bar in code to allow execution. 7 28
Problem copying record details to a new record 5 9
Dcount help 2 14
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question