Link to home
Create AccountLog in
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Avatar of udanabanana
udanabanana🇦🇺

VB6 timer VS Windows API
I have  the option to use VB6 standard Timer function or use windows API timer
I'm asuming with Windows API, my application is totaly inactive till next call. but with timer my application is active all the time. There fore Im saving CPU time? Am I correct?

This appliction suppose to run 24hrs 7days and check file changes in every 5 seconds in certain directories and update data.

Which method is better?

Thanks experts.
Udana

Reference
API vb source code(http://www.candsi.com/apitimers.zip)

Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of seshu123seshu123

Hai,
  use the timer api for your code.  it is fast over the timer control in vb.


Seshu123

Avatar of inthedarkinthedark🇬🇧

Your app is active with either timer or using SerTimer API. But there is a thrid method which uses much less CPU resources:

Facts:

1) SetTimer API is limiting in that it needs a sub within a standard module, so if you need to build timed events into a class module it is not the best choice.

2) SetTimer is probably more acurate.

3) A control loop is best where you need only control one timed operation. Best becuase if does not need to call a sub each time it is called. When the CPU calls any sub or function it needs to place a whole bunch of stuff on the call stack and also save memory registers AX,BX,CX,DX,EX etc. onto a stack.  It also needs to pop parameters onto the stack. So not calling a sub/function is alwyas way quicker.  So the best method for letting your app just die and wake up every few seconds with minimal CPU usage is to use a control loop likes this:

The following code will only use about 1 second of CPU time very week or so.

Dim dtRestart As Date
Const OneSecond As Double = 1#/(24# * 60# * 60#)

Do
     dtRestart = Now +  OneSecond * 5 ' Set time for next activity
     Do While Now < dtRestart
         Sleep 2000 ' it is dangerous to sleep for much longer as your app can hang windows if this number too large
         DoEvents ' Must have this so that other stuff can work
     Loop
     
     Call PeformStuff ' Do whatever you need.
Loop

Notes:

The function Now is very fast but can be improved on speed. I rewrote a version of now GetNow which uses GetTickCount API and works much faster than VB's Now (how weird is that).

You can create version of the above loop which is event better for CPU speed using TickCount The following code will use virtually no CPU time.

Const lInterval As Long = 5000
Dim lNextActivity As Long
Dim lLastTicks As Long
Dim lTicks As Long

Do
     lLastTicks = GetTickCount
     lNextActivity = lLastTicks + lInterval
     Do
         lTicks = GetTickCount
         If lTicks > lNextActivity Then
             Exit Do
         End
        ' handle tick clock reset by Windows every few weeks (39 days I think)
         If lTicks < lLastTicks Then
             Exit Do
         End If
         lLastTicks = lTicks  
         Sleep 1000
         DoEvents ' Must have this so that other stuff can work
     Loop
     
     Call PeformStuff ' Do whatever you need.
Loop

I presume your are using the NotifyChange series of API calls? Which makes sense over using repeated directory scanning.




Avatar of inthedarkinthedark🇬🇧

Woops....small bug

         If lTicks > lNextActivity Then
             Exit Do
         End If  <<<<<<<<<<< The If was missing

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of jmundsackjmundsack🇺🇸

Also, since your stated goal is...

    "This appliction suppose to run 24hrs 7days and check file changes in
    every 5 seconds in certain directories and update data."

...you might consider a solution that uses the FindFirstChangeNotification API:

http://vbnet.mvps.org/index.html?code/fileapi/watchedfolder.htm

HTH-Jon

ASKER CERTIFIED SOLUTION
Avatar of inthedarkinthedark🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of udanabananaudanabanana🇦🇺

ASKER

inthedark
yup your method is nice and simple. but whats wrong with my timer dll?
_________________________________

jmundsack
FindFirstChangeNotification is looks excellent. However still I want to use some sort of timer for my other events.
at the momemnt I use GetFileTime and check for the last modified date. I guess I can replcae that with FindFirstChangeNotification ?
or can I? With FindFirstChangeNotification; in case if my application down for certain time, we wont able to detect any changes to file during the down time? is it? Is there any method to check Filechange in relavant to specific time?


Avatar of udanabananaudanabanana🇦🇺

ASKER

Im quite New to useing this API thing... so Im bit scared... Whats your advise, is it good to use API even if I can do it in VB?

Im using API for for few things in this application.
I found this compiled version is terminated and generated windows kernal error in my client pc.
I cannot debug since this my client pc doesnt have VB installed.
but I tested in 3 other machines (all with vb installed) and works fine.
so this bugeered me big time...

Udana

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Hai,
   Then use the timer control in your application.  it is very easy to use.  because you are new to api then don't go for that.  try to use the timer and after you familiar with the API then you can  change that to API.


Seshu123

Avatar of udanabananaudanabanana🇦🇺

ASKER

hi Seshu123
I been using timer control few years.
I would like to learn more about API.
In future(soon) I want to develop this program as a windows service.

Is there any significant advantage using API over Timer Control?
 
thanks Udana

ya,
  The API is more faster than the normal Timer Control.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of udanabananaudanabanana🇦🇺

ASKER

Thanks guys for the help.

inthedark, I accept your solution as the best
grade B cuz solution is not customised, no feedback for the questions

Thanks again

Udana

Avatar of inthedarkinthedark🇬🇧

Sorry been away.  For future reference, the reason why a loop is better for a single background process is that when any Sub or Function is called the cpu is put to a lot of effort a timer is no exception.  The CPU has to add the return address onto the stack, save a bunch of CPU registers on the stack AX,BX,CX,DX,EX, etc, then save passed paramters onto the stack. After doing all of that each non static variable used by the timer is allocated some memory, then variables are initialised. At the end of the the sub it does it all again but in reverse.  CPUs are so quick these days so nobody cares, but that is how somepeople think, if you want to be better you need to start caring.
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.