Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat a task non-stop

Status
Not open for further replies.

bvdv

Programmer
May 20, 2001
4
NL
How can I create a program which repeats a task as fast as the processor can? I tried to use a loop in my program (Goto or While) but my computer gets stuck then. Timers can do their tasks once per milisecond and the proceccor should be able to run much quicker than that. I tried to add more timers in my program (repeating the same task) but maybe someone knows a better solution.
 
Try using the same do-while-loop you where using but add a Do Events at the end of it.
That will execute your task without won't stuck.

Ex:
do while (Condition)
(intructions)
Do Events
Loop
 
With time-critical code a DoEvents statement actually defeats the purpose, because it tells Windows to put your app on the 'back burner' to process other pending tasks before returning. If the loop is causing your computer to hang (and you've given it sufficient time to complete), I'd be surprised if there's any other reason except that the While condition never becomes true. An example of this is something I do to myself all the time:

Dim RS As ADODB.Recordset

Do While Not RS.EOF
'Statements
'More Statements
Loop

With a DoEvents in this loop or not, it will never exit unless RS.EOF will at some point be a True statement:

Do While Not RS.EOF
'Statements
'More Statements
RS.MoveNext
Loop

As far as the procedure running as fast as the processor can, I think fine-tuning your code and running as few background apps as possible can get you close. Since your VB app is a Windows thread, Windows reserves the right to give priority to someone else. You can try to make up for this with tecniques like using Long (32 bit) integers instead of Integers, using functions like Left$() and Mid$() instead of Left() and Mid(), and using short-circuit evaluation in Select Case statements. In the Ctrl+Alt+Del program list (Win9X) the only programs that need to be running are Explorer and Systray. Closing the others can speed things up. Finally, if you're REALLY concerned about speed, consider using a different language like C/C++ or assembly. VB is just too high-level to compete in a code race.

HTH.

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top