Check out the Sleep API. I would suggest that you place the sleep in a loop with a doevents for parts or your total delay so the program does not become unresponsive. Meaning you want a delay of 10 seconds which equals 10000 with the sleep api...
[tt]
For I = 1 To 100
DoEvents
Sleep 100
Doevents
Next I
[/tt]
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
API viewer is in Visual Studio Tools (or VB6 Tools)
________________________________________________________________
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.'
Timer returns the number of seconds elapsed since midnight, so just get the current timer and then loop until the new timer catches up to that time (plus however many seconds you want to delay).
To start with, when using a timer especially one that has a short interval you should do something like...
[tt]
Private Sub Timer1_Timer()
Timer1.Enabled = False
'Do your work here
Timer1.Enabled = True
End Sub
[/tt]
The reason for this is so you do not build up timer events in your stack. This can easily happen if you have a doevents contained within your timer that has a lot of processing to do. By a lot of processing I mean updating a display, reading from a disk, or from a database.
Now I am going to take a guess and say that you have two objects on the screen that you want to move simultaneously or in a reaction turn based event or very close to it and you are using one timer to control both. If something like this is true then you may want to use multiple timers. Perhaps one for each object. Since we are clueless here in exactly what you are trying to do, I though I would take a shot in the dark.
I use the following in it's own module (I call it modDelay). To use this you need to do a few things:
1) Call the sub SetTimerFreq() at the start of your program. This reads the clock frequency of the computer that is running the program. You only need to run this once during the program (although more than once doesn't hurt anything).
2) Call the sub Delay to delay for a given number of mSecs.
ex; call Delay(500) delays for 500 mSecs. This routine has a DoEvents built in, so other processes can run.
3) Use the TimeOut function in a loop to perform other activities while looping for a specified time. If you pass the optional varMsDelay value to the function the timeout is set to that many mSecs, otherwise, a previously set timeout is checked. The parameter intTimerNum allows you to run multiple timeouts at one time. As written in this example, 25 timers can be handled. Note that TimeOut requires a DoEvents in your looping code if desired. Use timeout as follows:
' The following boolean is set to false when timer 1 is set to 1 Sec (1000 mSec)
blnExitLoop = TimeOut(1,1000)
Do While blnExitLoop = False
' Things you want to happen go here
' The boolean var is set true if the timeout has expired
' Otherwise it remains false
blnExitLoop = TimeOut(1)
' Note do events is required with TimeOut
DoEvents
Loop
I have measured these functions, and found them to be accurate within approx. 1 mSec.
' Declare API timer clock functions
Public Declare Function QueryPerformanceCounter Lib "kernel32" (curCount As Currency) As Boolean
Public Declare Function QueryPerformanceFrequency Lib "kernel32" (curFreq As Currency) As Boolean
' Declare global var to hold QPCounter frequency value
' NOTE: Set this var in the main program startup routine by calling sub SetTimerFreq
' This is used in all time calcs. and permits routines to be used on different speed CPU's
Global gcurQPFreq As Currency
Public Sub SetTimerFreq()
QueryPerformanceFrequency gcurQPFreq
End Sub
'******************************************************************************************
'* Name: Delay(lngMsDelay As Long)
'* Function: Delay a specified time in milliseconds
'******************************************************************************************
Public Sub Delay(ByVal lngMsDelay As Long)
Dim curStart As Currency ' Start time
Dim curEnd As Currency ' Ending time
Dim curCurrentTime As Currency
QueryPerformanceCounter curStart ' Get the starting time
curEnd = curStart + (lngMsDelay / 1000 * gcurQPFreq) ' Calc actual end time
Do ' Loop until end time is passed
QueryPerformanceCounter curCurrentTime
DoEvents
Loop While curCurrentTime < curEnd
End Sub
'*********************************************************************************************
'* Name: TimeOut(Optional varMsDelay As Variant) As Boolean
'* Function: Determines if a specified timeout has been passed.
'* intTimerNum determines which timer to check (1 thru 25 as set in array)
'* varMsDelay sets a new value for the timer. If this value is omitted
'* then the timer is checked. Function returns True if timeout has expired
'* and false if not expired or set to new value
'*********************************************************************************************
Public Function TimeOut(intTimerNum As Integer, Optional varMsDelay As Variant) As Boolean
Static curEnd(1 To 25) As Currency ' Holds a previously set end time
Dim curCurrentTime As Currency ' Holds current clock count
Dim curDelayCounts As Currency ' Holds number of delay counts
' Safeguard against invalid timer index
If (intTimerNum < LBound(curEnd)) Or (intTimerNum > UBound(curEnd)) Then
TimeOut = True
Exit Function
End If
If (IsMissing(varMsDelay) = True) Then
' Check Timeout that was already started.
QueryPerformanceCounter curCurrentTime ' Get the current clock count
' Set return value to timeout condition (if current time is >= prev set end then return true)
TimeOut = (curCurrentTime >= curEnd(intTimerNum))
Else
' Get a new End time.
curDelayCounts = varMsDelay / 1000 * gcurQPFreq ' Convert time to clock counts
QueryPerformanceCounter curCurrentTime ' Get the current clock count
curEnd(intTimerNum) = curCurrentTime + curDelayCounts ' Calc new end time in clock counts
TimeOut = False ' Set return value
End If
Ok...
[tt]
Private Sub Timer1_Timer()
Dim I As Integer
Timer1.Enabled = False
'show picture
For I = 0 To (NumberOfSecondsToDelay * 10)
Doevents
Sleep 100
Doevents
Next I
'hide picture
Timer1.Enabled = True
End Sub
[/tt]
Would something like this work if you made the interval of the timer very short?
Line (linex, liney)-(linex +1, liney -7), &HFF00&, BF
Line (linex, liney)-(linex +1, liney -7), &H0&, BF
I'm trying to make the black line cover up the green one, so what would be the best way to make a delay inbetween the two. A sleep timer doesn't work because it delays everything else in the progam.
I am still having trouble with the delay for this game. I am currently using the following code to change the picture property of an image box (delaying between each change).
I want to stop using the sleep api because it is causing problems in other sections of the game. Does anyone have any suggestions?
Along the same lines of peter11's question, I wondered if anyone could provide some guidance on how I might set a delay based upon user input. The input range is 100-600000 milliseconds. Will the sleep function work for me?
Thank you.
Yes, and please next time start your own thread and if you want include a reference to the thread you are talking about. (All you need to do is copy the thread-xxxx listed at the top)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.