Hi all.
I am trying to create a console app that will allow me to control another program (guess which one
)
I found the documentation here:
https://documentation.xojo.com/topics/threading/running_code_periodically_with_a_timer.html#using-timer-in-a-console-project
But need a little bit more of an explanation on some things like:
Add the Action event to the App object and put this code to initialize the Timer and map its Action event to the TimerAction method:
// is this the App.Run?
MyTimer = New Timer
MyTimer.Period = 1000
MyTimer.RunMode = Timer.RunModes.Multiple
AddHandler MyTimer.Action, AddressOf TimerAction
While True
App.DoEvents
Wend
and
Now add the TimerAction method:
// i canât seem to add an event; where is it? Where do I put this?
Public Sub TimerAction(sender As Timer)
Print("Timer called.â)
End Sub
Sorry for the dumb questionsâŠ
Regards
If you are creating the timer manually you need to create the TimerAction method yourself. It is the method referred to here:
AddressOf TimerAction
If you add a timer to your application (using drag and drop) then you are able to add a method to it using the IDE. You can then refer to the CustomTimer (or what ever you name it). to configure it and then add Action to the CustomTimer Object.
Hi Ian.
Sorry if I seem dense but I am creating my console app in the same manner as the first section of code shown.
My issue is the AddHandler / Address of lines.
If the timer was in a Desktop app, I know I would use the Timer.Action Event. No problem there.
So how do I put the MyTimer.Action actually into the code?
Iâm watching one of the tutorials on youtube as well to help try and clear this up for me, so Iâm NOT sitting on my hands.
Regards
Hi Ian.
I think I figured it out.
In my app, I had to create a Method called âTimerActionâ
Then, in this case, and for testing I did a print, which shows up in the terminal window due to the
While True .. Wend
Now I have a request for clarification.
What do the AddHandler
and AddressOf
mean.
The explanation in the docs wasnât very clear to me.
Regards
Just having a method with that name on its own would not link the event to the timer. The AddHandler is what does that. For example, you could have a method called Banana and use the following AddHandler line to link the event to the method.
AddHandler MyTimer.Action, AddressOf Banana
AddHandler programmatically links an event, in this case a Timer Action event, to a method, in this example Banana.
After this line Banana is run when MyTimers Action event is Raised.
You must also use RemoveHandler when you are done.
Hi Ian.
Ok. Letâs see if I get this.
AddHandler says âWhen the Run event executes, you go to the MyTimer Action and do what is in thereâ. Am I thinking correctly?
But I am still fuzzy on the AddressOf portion and what IT does.
Regards
No.
Addhandler says âWhen the specified event (timer.action in Ianâs example) fires you go to this method (Banana in this example) and run the code that is there.â Nothing to do with the thread.
1 Like
Address of returns a memory reference to the method, a bit like a C pointer, but rather a Deligate which is a method with a signature.
What Tim said is correct. There are two separate parts, the event which is Raised or Fires. AddHandler provides the Method to call when it does
https://documentation.xojo.com/api/language/addressof.html#notes
One âfinalâ questionâŠ
(Yeah, rightâŠ
)
I read in the documentation that there is, for console apps, something called âDaemonizeâ, which by my understanding means ârun alwaysâ in the background.
In one of the tutorials on Youtube,
There are lines showing:
If Not App.Daemonize Then
System.Log(System.LogLevelCritical, "Could not daemonize the applicationâ)
Return -1
End If
Can someone explain HOW this works?
Regards
Typically daemon is a unix term, windows calls them services. They run in the background and donât have a user interface. The only one Iâve created with Xojo was used by a web server to perform randomisation service for a clinical trial. That was so long ago I canât really help you. It is likely worth starting a new discussion, rather than expecting people to read something new in an existing topic.