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

Timeout problem 1

Status
Not open for further replies.

aliinal

Technical User
Jun 26, 2001
104
TR
Hi!

In a do loop statement i want to control timeout. FOre example 5 seconds.

i use datediff but it does not work in runtime (It works if i debug) I use Doevents in the do-loop also.

can anyone advice me something?
 

Dim Test As Boolean, StartTime As Date, CheckTime As Date

StartTime = Now

Do While Test = False

CheckTime = Now

If DateDiff("s", StartTime, CheckTime) > 5 Then Test=True

Doevents

Loop
 
Set a Timer control to 5000ms. Declare a Boolean value that will remain in scope when the Timer event fires. Make the Timer's Timer event change the both Boolean to True and the Timer.Enabled property to False. Right in front of your Do loop, set the Boolean to False and enable the Timer. In your Do loop, check the Boolean value after your DoEvents. If the Boolean is True, Exit Do.
 
Oh i think i couldn't explain...
That's what i am doing now exactly. But it refuses to work in runtime.

when i debug step by step (F8), it works great. It returns a timeout. But if i do not debug, it does not response with a timeout. When i pause and begin step by step execution with F8 then it realizes that timeout is over and responses with a timeout.

But again thanks for your posts. But i'm asking you for another suggestion.
 
I've never been a real fan of using Date and Time functions with respect to timing issues. Since in this case, it seems that you want to enter into DoEvents loop for 5 seconds, I would control the timing using the GetTickCount API, and my sub would look something like the following:

In the Declarations section of the form, declare the following API

Private Declare Function GetTickCount Lib "kernel32" () As Long

and the sub

Private Sub DoEventsLoop (ByVal NumSeconds as Long)

Dim StartTime As Long
Dim DoLoop As Boolean
Dim TimeDiff As Long
Dim EndCount as Long

StartTime = GetTickCount
EndCount = NumSeconds * 1000
DoLoop = True
Do While (DoLoop = True)
DoEvents
TimeDiff = GetTickCount - StartTime
DoLoop = (TimeDiff < EndCount)
Loop

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
that's it CajunCenturion. its working great.
thanks
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top