Schedule a Method Daily By Time

Hi,

I am wanting to create a method that will run at a given time of the day, The method itself is easy, but I’m not sure how to get the time to always run in the background of my webapp.

Thanks in advance for any help.

Create a helper and run it on the server via crontab.

Can this be done within Xojo?

A simple solution is to add a Timer in App and check in the action of the Timer if you have to execute the task. In order for the Timer to work continuously it is necessary to prevent the App to quit.

  1. Add a Timer property to the App
Private Property AppTimer as Timer
  1. Add the Timer action method

[code]Private Sub TimerAction(sender As Timer)
//This method is executed every minute
//Check the time (hour and minute) to schedule the task

End Sub[/code]

  1. In App.Open prevent the App to quit and initialise the Timer
App.AutoQuit=False

AppTimer=New Timer AppTimer.Mode=2 AppTimer.Period=60000 AddHandler AppTimer.Action, AddressOf TimerAction

Our experience has been that it would be best to offload things like this to a console app. On Xojocloud cron is not available so I experimented and have been able to use one of the cron websites to access my web app on an interval using handleurl to fire off a console app. Don’t have it in production yet. And there are fees involved with using the cron website. But I think it will work well.

setcronjob.com

What I did in that situation was create a service app that is installed as a service on my windows server and pings a url on schedule using a timer. It works and it’s free.

[quote=371426:@Alain Clausen]A simple solution is to add a Timer in App and check in the action of the Timer if you have to execute the task. In order for the Timer to work continuously it is necessary to prevent the App to quit.

  1. Add a Timer property to the App
Private Property AppTimer as Timer
  1. Add the Timer action method

[code]Private Sub TimerAction(sender As Timer)
//This method is executed every minute
//Check the time (hour and minute) to schedule the task

End Sub[/code]

  1. In App.Open prevent the App to quit and initialise the Timer
App.AutoQuit=False

AppTimer=New Timer AppTimer.Mode=2 AppTimer.Period=60000 AddHandler AppTimer.Action, AddressOf TimerAction[/quote]

This is working ok.

If I get any issues I will try

Thanks

Also remember to handle things if your app is down when the method is meant to run and to not run again if you do it via a time > x check and its already been run that day or it will happen every time you restart your app. Saving the daily state of the task to disk is advised to get around these problems :slight_smile: