Run a function at a certain time of day

Post your Gambas programming questions here.
Post Reply
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Run a function at a certain time of day

Post by sarpomira »

Hi All,

I'm struggling a bit with this one.
Assume I have a subroutine called "run_analysis()"

Whats the syntax to call this routine every day at 1:30 PM ?

Bonus question: :lol:
Whats the syntax to call this routine every day at 1:30 PM ... on weekdays only ?

thanks for any help
cheers
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Run a function at a certain time of day

Post by cogier »

Here is my possible solution. A timer is setup to trigger every minute. It checks to see if today's date is a weekday and if it is then it checks to see if the time is 13:30 and if so runs the required routine.

Timer1 As Timer

Public Sub Form_Open()
  
  With Timer1 = New Timer As "Timer1"
    .Delay = 60000 ''Delay of 1 min
    .Trigger
    .Start
  End With
  
End

Public Sub Timer1_Timer()
  
  If WeekDay(Date(Now)) > 0 And WeekDay(Date(Now)) < 6 Then     '' 0 is Sunday, 7 = Saturday
    If Format(Time(Now), "h:nn") = "13:30" Then run_analysis    '' Is it 1:30PM, if so run the routine
  Endif
  
End

Public Sub run_analysis()
  
  ''Do important stuff!
    
End
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Re: Run a function at a certain time of day

Post by sarpomira »

To clarify the logic....
When the [START] button is clicked, the code in the button event is an endless loop which monitors stuff (say... greenhouse environment).
Within the loop is a the "time of day" snippet which calls a subroutine to save some data to a file every day at 1:30 pm.

Thanks gentlemen, I think this is what I was looking for...

 If WeekDay(Date(Now)) > 0 And WeekDay(Date(Now)) < 6 Then     '' 0 is Sunday, 7 = Saturday
    If Format(Time(Now), "h:nn") = "13:30" Then run_analysis    '' Is it 1:30PM, if so run the routine
  Endif


Cheers
Post Reply