Timers in Console Apps

Hello everyone!, I’m new to Xojo and would like to learn how to develop console apps for Linux using Xojo. I’ve built a few based on the examples and reading the manuals, but I had a question about timers in console apps.

Could someone please share or point me to a reference on the best practices way for adding timers to Console apps?

Looking around I found this reference:

http://docs.realsoftware.com/index.php/ConsoleApplication.DoEvents

But, I haven’t been able to figure out how I add the timer to the console app in Xojo IDE or if the timer is defined in the code behind for the Run Event Handler

Dim TMR as new myTimerSubclass
TMR.mode = 2
TMR.period = 1000

Do
App.DoEvents
Loop

'Timer.Action
'StdOut.WriteLine(Str(new Date))

Any help would be appreciated.

You need to activate the timer.

TMR.enabled = true

Then timer will fire… Also I personally try avoiding TMR.mode = 2 and use TMR.Period = TMR.ModeMultiple for keeping code more readable when your project gets bigger.

[quote=13349:@Jeff Fisher]Hello everyone!, I’m new to Xojo and would like to learn how to develop console apps for Linux using Xojo. I’ve built a few based on the examples and reading the manuals, but I had a question about timers in console apps.

Could someone please share or point me to a reference on the best practices way for adding timers to Console apps?

Looking around I found this reference:

http://docs.realsoftware.com/index.php/ConsoleApplication.DoEvents

But, I haven’t been able to figure out how I add the timer to the console app in Xojo IDE or if the timer is defined in the code behind for the Run Event Handler

Dim TMR as new myTimerSubclass
TMR.mode = 2
TMR.period = 1000

Do
App.DoEvents
Loop

'Timer.Action
'StdOut.WriteLine(Str(new Date))

Any help would be appreciated.[/quote]

You might have a look on this page: http://docs.realsoftware.com/index.php/AddHandler

It would be best if you use addhandler for your timer event.

Great! Thanks Amando and John, I took your suggestions and it works great now, appreciate your help.

Function Run(args() as String) As Integer
Dim myTimer as new Timer
myTimer.mode = Timer.ModeMultiple
myTimer.Period = 1000

AddHandler myTimer.Action, AddressOf myTimerAction

Do
App.DoEvents
Loop
End Function

Protected Sub myTimerAction(sender As Timer)

StdOut.WriteLine(Str(new Date))

End Sub

Opps, it worked without myTimer.Enabled = True, but I added it for best practice sake:

Dim myTimer as new Timer
myTimer.mode = Timer.ModeMultiple
myTimer.Period = 1000
myTimer.Enabled = True

AddHandler myTimer.Action, AddressOf myTimerAction

Just a small comment, the updated versions of the docs are at https://documentation.xojo.com

https://documentation.xojo.com/index.php/AddHandler

The old one says "Do forget this parameter (even if is not used) or you will get a compile error. "
The current says "Do not forget this parameter (even if is not used) or you will get a compile error. "

For example. :wink:

Timers are weird in Xojo. The defaults are Enabled=true, Mode = ModeMultiple and Period = 1000.

That means, that if just create a timer, it’s ticking to fire up.

Good to know, thanks for sharing that