Looping Efficiency - Is There A Better Way?
Newbie Programmer here looking for advice: I have a small app that checks a sorted array with a date/time field as a "trigger" (only 3 records entered to test).
Thing is, while this "check loop" is running it may be too simple to be practical - the app eats up CPU cycles while just "checking" for the next trigger time, which could be set for hours or days away.
Is there a more efficient way to program this? Like a "sleep time" function or a better known method for "checking" only once a minute or so? (Even "high level" advice would be appreciated.) Thanks!
ex:
Code:
X = 0
Do While X <> 3
Y = X
Do While Y = X
If Date.Now > TableData(X).TriggerDateTime Then
[Action Code]
X = X + 1
End If
Loop
Loop
---------------
ENV
mailto:[email protected]
Re: Looping Efficiency - Is There A Better Way?
I have no idea what language nor OS you are using. But if it is on Windows, and you are using a language that can use API calls, then you can call Sleep() for as long as you want. That pauses your whole app, so be aware of that. But if that doesn't do anything, then that should be sufficient.
- ØØ -
Re: Looping Efficiency - Is There A Better Way?
Just using VB.NET. I wouldn't want it to "hang" the rest of my application, but I just notice that while this is running (and waiting) my CPU is at 50% usage...and all it's doing is looping/waiting...