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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put a delay for a second into a programme

Status
Not open for further replies.

Danthemightyone

Technical User
Feb 18, 2003
3
IE
I am writing a programme to recieve information from a camera through the serial port, however to computer is too fast and the programme is finished before any charactor is recieved from the camera. Does anyone know how to insert a delay of a second into the programme to help the camera catch up with the computer?
 
You can take a look at the Sleep API which can provide you some control over sleep intervals. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
If you are using MSComm control for reading te serial port, you might have a look at VBHelp for the ONComm event
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks guys for your help, but I still cannot get it working. I've inserted a msgbox after my MSComm1.Output command so that there is a delay, however this is very innefficient.

CajunCenturion, I could not get sleep API to work on VB6.

johnwm, which piece of code in ONComm event do you think would be uselful?
 
To any API, you first need to declare the function. I usually declare API functions in a module, at the top in the declarations section.

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

Then within your code, call the function

Sleep 1000 ' Sleep for 1 second

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I had to do a delay to allow something to occur and found that the Sleep API function event tied up the CPU so the event I needed to occur couldn't occur.

I ended up using a Timer control. This meant that the flow of control had to be changed as follows:
> In the subroutine which needs to wait.
> Do the actions needed before the external event.
> Enable the timer.
> In the timer's Timer event:
> Disable the Timer.
> Do the rest of the events from the original subroutine.

If it is possible that the external event has not occured after 1 second, you can (hopefully) insert a check for it in the timer control's Timer Event:
> If event has occured:
> Disable the Timer.
> Do the rest of the events from the original subroutine.

The above will cause the Timer to be triggered every second until the event has occured. In which case, go ahead and set the interval to less time if you want. Our intervals are set to 0.1 seconds so as soon as the action has occured, the processing continues.

FYI: One of the solutions we tried required that I set DoEvents to True while waiting for the event to occur. I found that this allowed my user to do things that couldn't be allowed at that point. We did not go down that route after we saw that.
 

check out this thread222-474702. Has a small discussion on suggested ways to use sleep.

Good Luck

 
>the Sleep API function event tied up the CPU

Au contraire. That's precisely what Sleep does not do.
 
Re: The sleep API does not tie up the CPU.

You're right. However, whatever the reason, from the time sleep was invoked until it came back, the event we were waiting for would never occur no matter how long the sleep was for.

FYI: The event we needed to occur was for a Crystal Report to finish initializing itself. Perhaps in VB if sleep is running and DoEvents is false, VB won't process background processes, I don't know.
 
VB is single-threaded. Sleep puts the thread to sleep. Whilst it is asleep it is doing absolutely nothing. It therefore cannot recieve or process any events.
 
I understand that while "sleeping" VB is inactive, but during that time am I right in thinking that the OS takes that opportunity to process other applications/processes?

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top