Visual Basic Classic
--
Questions
--
Followers
Top Experts
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.
 use the timer api for your code.  it is fast over the timer control in vb.
Seshu123
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.
     If lTicks > lNextActivity Then
       Exit Do
     End If  <<<<<<<<<<< The If was missing






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
  "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 FindFirstChangeNotificatio
http://vbnet.mvps.org/index.html?code/fileapi/watchedfolder.htm
HTH-Jon
yup your method is nice and simple. but whats wrong with my timer dll?
__________________________
jmundsack
FindFirstChangeNotificatio
at the momemnt I use GetFileTime and check for the last modified date. I guess I can replcae that with FindFirstChangeNotificatio
or can I? With FindFirstChangeNotificatio
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

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.
  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
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
 The API is more faster than the normal Timer Control.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
inthedark, I accept your solution as the best
grade B cuz solution is not customised, no feedback for the questions
Thanks again
Udana
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.