|
-
Apr 9th, 2011, 08:16 PM
#1
testing time delay
i would appreciate if anyone can see any possible cases where this routine could fail or error
or other possible improvements
the idea is to limit the maximum number if iterations (user set maxe) per hour, the test below i have changed to minutes, for testing purposes,
vb Code:
Sub test()
Dim strtime As Date, e As Long, maxe As Long
strtime = Now
maxe = 850
For e = 1 To 100000
If e Mod maxe = 1 And Not e = 1 Then
' .Disconnect
Do Until DateDiff("s", strtime, Now) > (e \ maxe) * 60
DoEvents
Sleep 3000
Loop
' .Connect
Debug.Print Now
End If
Next
End Sub
within the application, this is actually in a do loop, e = e +1, with unknown maximum, but i would expect to always be less than 100000
the computer is unlikely to be used for other purposes while this routine is running
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 10th, 2011, 01:30 AM
#2
Re: testing time delay
The only thing I can think of is what happens if the system is running when Daylight Saving starts or stops and the system clock goes forwards or backwards by an hour ? (or some intrepid user decides to change the clock whilst the process is running)
Apart from that, I assume you're aware of the 'limtations' of the Sleep Function (http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
-
Apr 10th, 2011, 02:14 AM
#3
Re: testing time delay
yeah daylight saving changes could be an issue as the program is left running overnight, i will have to sort something on that (maybe "do not use" those 2 days of the year)
precise start time is not relevant as long as it is greater than one hour from previous start, it is possible sometimes that it will take more than one hour for max number of iterations, processing time can vary considerably, depending on user options and other outside factors, which reminds me of some other not handled error possibility
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 10th, 2011, 02:55 AM
#4
Re: testing time delay
One other thought; I'm not sure whether the DoEvents is a good idea.
I don't know too much about the Sleep API but I'm guessing that when the time has expired and the thread is schedulable again, any outstanding events will be serviced, which would do away with the need for DoEvents.
Depending on user interaction and the design of your application, there may be a chance of some non-reenterable code being re-entered as a result of a call to DoEvents, which may cause problems. (eg the user idly clicking on some button or other) Such risks can be mitigated against by disabling buttons etc once the user has started the process running.
The 'Daylight Saving' note was a bit tongue-in-cheek, but since you are not using the absolute values of Date and Time, and are only interested in relative time, you could, perhaps, use the QueryPerformanceFrequency and QueryPerformanceCounter APIs instead of 'Now', which would make it independent of System Date / Time.
Then there's the obvious things, like validating all User input etc.... but I'm sure I don't have to mention that
-
Apr 10th, 2011, 06:53 AM
#5
Re: testing time delay
most of the input has been in use for some time, so should be ok, just a few modifications, which i am sure are likely to come back and haunt me, despite trying to make sure none of the additions will cause problems with the existing code, and being unable to test large parts of it on my system, does not help
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|