VB Wait command


Thread Starter


I am writing a program in visual basic that pulls ati7 from a modem and displays it in a text box. I have the code working but I had to use two command buttons to make it work. The problem is that if i put all the code for one button the modem doesn't have time to respond before vb executes the next command. Does anyone know a command that will make the program wait approx 1 or 2 seconds before executing the next statement?
I've run into this same problem before. I am not sure if VB has a WAIT or SLEEP command, but I found another way to do this.
Place a Timer control in the form (it looks like a stopwatch, and I believe it is part of the normal VB components). The default name will be Timer1. In your function where you want the delay, put in the following code:

timer1.interval = 2000 'time is in msec
timer1.enabled = TRUE 'calls the timer function

Then, in the Timer function, put the rest of your code in here. When you enable a timer in a function, the rest of the code within that function is still executed. Then, the timer is called and the timer code will execute every 2000 msec. So the first time, just let the function pass through. The second time, execute the rest of your code or call a subroutine that will do this, and set the enable of the timer to FALSE to stop it. You can let it pass through first by using an if-else or case-select command. But your counter variable must be outside of the timer function, otherwise it will initialize itself everytime you declare it.
I might be taken a very long route to do a simple task, so if anyone else has any input I could use the help as well!

José Luis Herrera

You could use win32 API that has a sleep function
First you declare the sleep function

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

Then in the place where you want the program to wait just call the function with the number of milliseconds to wait.

There is something you have to be aware of, if your program is only running in one thread then the program won't be able to do anything else while it is sleeping, if you need to be able to do something else while waiting you better use timers.

José Luis Herrera

Why not add a Do Until Loop and wait for the modem to respond before you request it to do something else? You should add a DoEvents command so that you can break out of the endless loop if the modem does not respond as well as start a timeout timer) that exits the loop.


Collin R. Campbell

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

Then in your code



Kevin MacMillan

Dim PauseTime, Start, Finish
PauseTime = 1 ' Set your 1 or 2 second value.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.

'Put your code here