Server side timer for web app

I need a server side timer that starts with App.Open and will manipulate a SQLite database every day at midnight. I added the following to the Event Handler:

Dim Timer1 As New Timer
Timer1.Period = 30000 (30 seconds)
Timer1.Mode = Timer.ModeMultiple
Timer1.Enabled = True

The problem is, how do I create an event handler for the Action event? In a desktop app when you drag a timer control to a page it automatically creates the Action event for you.

Use AddHandler to tie the Timer.Action to a method in app object.

I think* your method needs a parameter

Sub timerAction(t as timer )
//Do stuff
End sub

Then

addhandler timer.action, addressof timerAction.

What is the uptime like for a cgi app? I see this working in a stand alone app, but a cgi app might get killed off and not be running when the timer should be executing.

I added a new Method called TimerAction with the parameters (sender As Timer) and then put the AddHandler statement in App.Open as Bob suggested. It compiles but the Timer doesn’t seem to fire. I have the Method set to send me a test email but I never get it. Am I missing something?

Here is the App.Open code:

dim Timer1 As new Timer
AddHandler Timer1.Action, AddressOf TimerAction
Timer1.Period=30000
Timer1.Mode = Timer.ModeMultiple
Timer1.Enabled=True

Try making the timer a property of the app.

Do dim Timer1 as new Timer would be

Timer1 = new Timer

I changed it to:

dim Timer1 As Timer

Timer1 = New Timer
AddHandler Timer1.Action, AddressOf TimerAction

Timer1.Period=5000
Timer1.Mode = Timer.ModeMultiple
Timer1.Enabled=True

No go. I’m not sure why it’s not firing…

Timer1 should be a property on the App object not defined locally. As soon as the open event is done Timer1 goes out of scope and becomes nil thus never firing (unless you just wrote it that way).

Usually cgi apps start and stop all the time. If it needs to be running at midnight you can have a script call the URL at midnight (or close to it). Script is not the right word but the proper term is escaping me at the moment (yay! It’s Friday!).

Bingo Bob. I made Timer1 a property and now it fires and works as expected.

Thank you all for your advice!

Just to pick up on this tread again.
I have similar requirement for a web email server that polls a db and if it finds a new record it grabs the data and uses it to create and send an email.

I have a timer that fires every 25 seconds like Phillip uses at 1701 to watch for the cgi file.
Ie if it’s missing the app shuts down.
It’s basically exactly as the above.

My problem is the cgi app closes, and so the timer stops, and so then does the polling.

I have a feeling that a webapp is not the right project type for this server?

Or is their away to make a cgi xojo app stay active?

Anthony how did every thing work out for you?

Greg could explain it better but the trick is to have a reference to a session that keeps it active. So as long as the session is alive the cgi app won’t die.

thanks Michel
Ill Play

I found this in the docs
ill play with this also

WebApplication.AutoQuit
Property (As Boolean)
aWebApplication.AutoQuit = newBooleanValue
or
BooleanValue = aWebApplication.AutoQuit
If True, the server will shutdown automatically when all sessions have ended, which is the default for CGI web apps. If False, the server will not shutdown. This is the default for stand alone web apps.

This absolutely works and solves my polling problem

it is an old topic but maybe it will be helpful for someone. In my case I wanted to have some background process importing something from an external MySQL database into Web App Sqlite database regardless there is any session active (import during night).

In short I wanted to be able to add regular timer to (windowless) App part. I did it this way:

  1. added new empty Class to the project
  2. set its Super: type to Timer and changed the name to MyTimer. Now it behaves as a timer, you can add “Action” handler to it standard way and so on…
  3. in App part I added new public property called “MyOwnTimer” with type “MyTimer” (the name of the class mentioned in #1 & #2 steps)
  4. in Open event of App I added (example):

if MyOwnTimer = nil then
MyOwnTimer =new MyTimer
MyOwnTimer.Mode = Timer.ModeMultiple
MyOwnTimer.Period=60100045 //action every 45 minutes…
MyOwnTimer.Enabled=true
end if

works perfectly… :slight_smile:

1 Like

Hello!!!
I am working with a webapp and also need a timer that works all the time, even if no session has been opened.

I follow Pawel Soltysinski criteria and I created “mytimer” as a class timer.

if app.appTimer = nil then
    app.apptimer = new mytimer
    app.appTimer.mode = timer.modeMultiple
    app.appTimer.period = 1000
    addHandler app.apptimer.[b]action[/b], addressOf app.apptimer_Control
    app.appTimer.enabled=true
  end if

the action event does not show as tab key is pressed and the error is:

Type mismatch error, Expected delegate Delegate(MyTimer), but got delegate Delegate() 

Regards!

The compiler error actually tells you what the problem is. One of the parameters should be a timer. So something like this:

Instance as Timer

That way in code you can do this:
instance.mode = timer.modeoff

It simply means you need to add

Sender as Timer

in your apptimer_Control method.

Thanks Michel, but I might be in my lower intellectual biorhythm, jajajajaja
in the app section of the webapp i place the following:

  • in the PROPERTIES of the APP section I create one that is name AppTimer as Timer

  • in the OPEN event of the APP, a call to the appTimer_Setup method

  • at the appTimer_Setup method, also in the APP section, i place the setting of the AppTimer

  • the AppTimer_Control method , too in the APP section, should display a single counter with the message box

the action event does not show in the list of available events of the AppTimer

Thanks in advance!