Author Topic: Recommended method for inserting program delays  (Read 3542 times)

0 Members and 1 Guest are viewing this topic.

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Recommended method for inserting program delays
« on: February 23, 2020, 10:14:05 am »
I wrote a program, that gets kicked off each day before 8:00am by the Windows Task Scheduler. The program waits until the clock indicates 8:00am and then it starts. The first thing the program does is pull some data from a website, parse out the info I'm looking for, then adds it to a file on the disk. Then I have it 'sleep' for 5 minutes and repeat the process of pulling, parsing, and saving the data every 5 minutes.

This all starts out just fine, but at random points in the day it starts to get data more or less than what the 5 minute delay would indicate. One day it pulled data at the exact time almost a dozen time. If the timestamps had always been more than 5 minutes, I could chalk it up to taking too much time processing the previous code; but not less, or multiples at the same time.

What is the recommended method for delaying a program for 5 minutes? I have tried both the _DELAY and SLEEP commands. Thanks, Mike

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Recommended method for inserting program delays
« Reply #1 on: February 23, 2020, 10:15:22 am »
Could it be caused by some kind of packet truncation? As in, looking for a terminating character that never made it through?
You're not done when it works, you're done when it's right.

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #2 on: February 23, 2020, 10:27:47 am »
I wouldn't think so; as that would only account for the timestamps being more than five minutes apart. But certainly not for the ones less than 5 minutes apart, or the ones that have the exact same time stamp. Thanks, Mike

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #3 on: February 23, 2020, 10:30:35 am »
I attached the output file from one day.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Recommended method for inserting program delays
« Reply #4 on: February 23, 2020, 10:37:45 am »
Would ON TIMER work?

I don't use it so I am not sure from experience but to me it seems the perfect tool for events to go off periodically.
« Last Edit: February 23, 2020, 10:43:40 am by bplus »

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #5 on: February 23, 2020, 04:23:35 pm »
Yes, the more I look at this, the more I'm thinking that just having a delay is at least at fault for pushing things out.  But that doesn't answer why I had that one day with multiple entries for the same time. That one has me stumped. I inserted 5 places n the code to capture a time stamp to be printed at the end of the program run. I'll see what that shows. But I'm thinking that implimenting a 'time check' would be a better way. Stay tuned, and thanks, Mike

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Recommended method for inserting program delays
« Reply #6 on: February 23, 2020, 04:32:22 pm »
Wouldn’t the easy way here be:

Start = 8 * 60 * 60 ‘8:00

DO
    IF TIMER >= Start THEN ‘it’s past 8:00
        Start = Start + 5 * 60 ‘add 5 minutes to next start timer
        ‘Other stuff
     END IF
    _LIMIT 1 ‘once per second is fine for this application
LOOP
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Recommended method for inserting program delays
« Reply #7 on: February 24, 2020, 03:04:45 am »
Basically, what Steve said.

A full day is based on 86400 timer intervals. (24 * 60 * 60)

myDelay = 5 * 60 ' 5 - minute delay.
DO
t! = TIMER
IF t! > TIMER THEN t! = t! - 86400 ' Midnight adjustment.
    IF TIMER - t! >= myDelay then
        ' .... DO SOMETHING...
    END IF
LOOP

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #8 on: February 24, 2020, 07:04:29 am »
Well, after fiddling with this for a few hours last night, and letting the test run overnight, I think I figured out what to do. The light bulb finally came on once I realized that all I needed to do was test the one's digit of the minutes to see if it a 0 or a 5. I know it uses more lines of code, but it is something I understand (math isn't my best subject!). Anyway, here is what I came up with. Now I just have to insert it in the proper places in my other program; and hope it works. Thanks, Mike

TimeRecheck:
SLEEP 30
HHMM$ = LEFT$(TIME$, 5): PRINT HHMM$
IF HHMM$ <> "22:45" GOTO TimeRecheck
PRINT
'
' This should probably be before the start of the looping
'
PRINT "Time to start the 5 minute counter"
'
loop1:
IF VAL(MID$(TIME$, 5, 1)) = 0 GOTO loop2
IF VAL(MID$(TIME$, 5, 1)) = 5 GOTO loop2
_DELAY 10
GOTO loop1
loop2:
PRINT TIME$ 'Do all the stuff here maybe??????
loop3:
IF VAL(MID$(TIME$, 5, 1)) = 0 OR VAL(MID$(TIME$, 5, 1)) = 5 GOTO loop3
GOTO loop1

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #9 on: February 27, 2020, 10:12:12 am »
So, from all the help you all have provided; I figured I'd provide my 'poorly hacked code'. I say 'poorly hacked' because, barring the function code (I got somewhere!), I'm sure what parts I actually wrote are far from the most optimal and efficient. But here is is anyway:

Code: QB64: [Select]
  1. [' Program to pull Gold and Silver Price data from the internet
  2. ' The program starts at 8:00am each day, and runs for 10 hours
  3. ' The program will capture data at 5 minute intervals, and store the data to disk
  4. ' The disk file is open and closed as each cycle occurs
  5. '
  6. ' This is version 9 of the program
  7. ' Program property of Mike Lawson
  8. '
  9. SCREEN 12 '640x480 16 colors
  10. WIDTH 80, 60 '80 columns & 60 rows
  11. COLOR 15 'Bright white
  12. DIM FullDate$(120), AuPrice$(120), AgPrice$(120)
  13. _TITLE "Date Time Web Pull ver 09 by Mike Lawson"
  14. '
  15. NewDate$ = RIGHT$(DATE$, 4) + LEFT$(DATE$, 2) + MID$(DATE$, 4, 2)
  16. FileName$ = NewDate$ + "WebPull.txt"
  17. '
  18. TimeRecheck:
  19. HHMM$ = LEFT$(TIME$, 5): PRINT HHMM$
  20. IF HHMM$ <> "08:00" GOTO TimeRecheck
  21. FOR N1 = 1 TO 120
  22.     'rerun:
  23.     loop2:
  24.     IF VAL(MID$(TIME$, 5, 1)) = 0 OR VAL(MID$(TIME$, 5, 1)) = 5 GOTO loop3
  25.     _DELAY 10
  26.     GOTO loop2
  27.     loop3:
  28.     SHELL _HIDE "CMD /C wGet.bat"
  29.     '
  30.     OPEN "bb1.txt" FOR INPUT AS #1
  31.     n = 1
  32.     loop1:
  33.     INPUT #1, TextLine$(n)
  34.     IF n = 3 THEN AuPrice$(N1) = MID$(TextLine$(n), 26, 7)
  35.     IF n = 4 THEN AgPrice$(N1) = MID$(TextLine$(n), 26, 5)
  36.     IF n = 6 THEN GOTO finish
  37.     n = n + 1
  38.     GOTO loop1
  39.     finish:
  40.     CLOSE #1
  41.     '
  42.     month$ = LEFT$(DATE$, 2): M = VAL(month$)
  43.     day$ = MID$(DATE$, 4, 2): D = VAL(day$)
  44.     day$ = STR$(D) ' eliminate any leading zeros
  45.     year$ = RIGHT$(DATE$, 4): Y = VAL(year$)
  46.     SELECT CASE M
  47.         CASE 1: Moon$ = "January"
  48.         CASE 2: Moon$ = "February"
  49.         CASE 3: Moon$ = "March"
  50.         CASE 4: Moon$ = "April"
  51.         CASE 5: Moon$ = "May"
  52.         CASE 6: Moon$ = "June"
  53.         CASE 7: Moon$ = "July"
  54.         CASE 8: Moon$ = "August"
  55.         CASE 9: Moon$ = "September"
  56.         CASE 10: Moon$ = "October"
  57.         CASE 11: Moon$ = "November"
  58.         CASE 12: Moon$ = "December"
  59.     END SELECT
  60.     PRINT WeekDay$(M, D, Y) + ", " + Moon$ + day$ + ", " + year$ + SPACE$(3); Clock$ + SPACE$(3);
  61.     PRINT "Gold =";
  62.     PRINT USING "$$####.##"; VAL(AuPrice$(N1));
  63.     PRINT "   Silver =";
  64.     PRINT USING "$$##.##"; VAL(AgPrice$(N1))
  65.     FullDate$(N1) = WeekDay$(M, D, Y) + " " + Moon$ + day$ + " " + year$ + SPACE$(3) + Clock$
  66.     '
  67.     'Output data to disk
  68.     '
  69.     AgDelta = VAL(AgPrice$(N1)) - VAL(AgPrice$(1)): AuDelta = VAL(AuPrice$(N1)) - VAL(AuPrice$(1))
  70.     OPEN FileName$ FOR APPEND AS #1
  71.     _TITLE "Date Time Web Pull ver 09 by Mike Lawson " + "Silver " + STR$(AgDelta) + " Gold " + STR$(AuDelta)
  72.     WRITE #1, FullDate$(N1), AuPrice$(N1), AgPrice$(N1)
  73.     CLOSE #1
  74.     loop4:
  75.     IF VAL(MID$(TIME$, 5, 1)) = 0 OR VAL(MID$(TIME$, 5, 1)) = 5 GOTO loop4
  76.     'GOTO loop2
  77. NEXT N1
  78. 'GOTO rerun
  79. ' --------------------
  80. ' Functions go here
  81. ' --------------------
  82. ' Weekday formatting function
  83. DEFINT A-Z
  84. FUNCTION WeekDay$ (M, D, Y)
  85.     IF M < 3 THEN M = M + 12: Y = Y - 1 'add 12 to Jan - Feb month, -1 year
  86.     C = Y \ 100: Y = Y MOD 100 'split century and year number
  87.     S1 = (C \ 4) - (2 * C) - 1 'century leap
  88.     S2 = (5 * Y) \ 4 '4 year leap
  89.     S3 = 26 * (M + 1) \ 10 'days in months
  90.     WkDay = (S1 + S2 + S3 + D) MOD 7 'weekday total remainder
  91.     IF WkDay < 0 THEN WkDay = WkDay + 7 'Adjust negative results to 0 to 6
  92.     SELECT CASE WkDay
  93.         CASE 0: day$ = "Sunday"
  94.         CASE 1: day$ = "Monday"
  95.         CASE 2: day$ = "Tuesday"
  96.         CASE 3: day$ = "Wednesday"
  97.         CASE 4: day$ = "Thursday"
  98.         CASE 5: day$ = "Friday"
  99.         CASE 6: day$ = "Saturday"
  100.     END SELECT
  101.     WeekDay$ = day$
  102. ' Clock formatting function
  103. FUNCTION Clock$
  104.     hour$ = LEFT$(TIME$, 2): H% = VAL(hour$)
  105.     min$ = MID$(TIME$, 3, 3)
  106.     IF H% >= 12 THEN ampm$ = " PM" ELSE ampm$ = " AM"
  107.     IF H% > 12 THEN
  108.         IF H% - 12 < 10 THEN hour$ = STR$(H% - 12) ELSE hour$ = LTRIM$(STR$(H% - 12))
  109.     ELSEIF H% = 0 THEN hour$ = "12" ' midnight hour
  110.     ELSE: IF H% < 10 THEN hour$ = STR$(H%) ' eliminate leading zeros
  111.     END IF
  112.     Clock$ = hour$ + min$ + ampm$
  113.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Recommended method for inserting program delays
« Reply #10 on: February 28, 2020, 03:43:33 pm »
With a SLEEP every 30 seconds, I'd be worried about this line...

IF HHMM$ <> "08:00" GOTO TimeRecheck

So what if the variable is measuring 08:01, 08:02, etc? Another problem with SLEEP, even if yo could depend on it to run on the minute and half minute exactly, what if your cat walks across your keyboard? Any key press moves the program immediately out of SLEEP. Now _DELAY 30 doesn't have that nasty key issue. It just ignores your cat and waits the 30 seconds. I guess _DELAY is more of a dog non-person.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #11 on: February 28, 2020, 08:19:40 pm »
It has been done - SLEEP changed to _DELAY. Thanks for the suggestion, Mike

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: Recommended method for inserting program delays
« Reply #12 on: March 01, 2020, 01:44:30 pm »
I also added a bit more code so I can see what the delta price is from the opening price. I'm going to do a live test this evening. Now I'm trying to think of how I can change the color of the text in the _TITLE string; green for a rising price and red for a falling price. Mike