Timer in a Module -How to

I’m trying to use a timer in a module and haven’t much clue on how it works…

I have added a timer to the module, then added an eventhandler action to it. And last made it readble/writable (just guessing that it is needed).

How do I address the timer from a Method in the same module?

If I try to do this in the Method:
Timer1.Mode=Timer1.Modesingle
or fiddling a bit by random and write (or other variations):
Module1.Timer1.Mode=Module1.Timer1.Modesingle

It results in an error:
This item does not exist
Timer1.Mode=Timer1.Modesingle '(with “Mode” highlighted)

I have searched, but not really found anything on this.

You must drag an instance of your timer over a window or a Container Control.

I am trying to group parts of code in order to make the programs more tidy. In this case I was trying to move everything that is involved in sampling a block of sound samples then processing them, into a Module. A module that I was supposed to be able to use in other programs by just copying the module. Timers are part of the routines.

Are you saying that I can not put a timer into a module? Then why can I drag one into the module? I can’t put the container control inside the Module either, it seems

If a module can not do what I want. Is there anything that can?
I just want to gather code in a convenient way.

I need to start a process (involving methods, properties, timers etc) and then to be able read the result. No other interaction until finished. Just start process -> read from memoryblock or array when finished. Keeping all these methods, properties and timers inside their own box so it is easier to find my way around among the rest of the code and to make it easier to re-use it in other programs I might need it.

I could compile it as a separate program or console app and run that (instead of what I thought could be gathered inside the module), but doesn’t feel perfect and I will have pick up the data via UDP/TCP, read from a file etc? No was to use arrays or memoryblocks. Hm. I don’t know…

Any ideas?

What you probably have is a subclass of a timer in the module - not an actual instance

Sounds to me like what you need is a property in the module that holds a timer and that timers action event then calls some method in the module

thats completely different than dragging one in

Where do I find out how one adds an action event to a timer, in a property, in a module?

Follow these steps:

  1. Create a property called ‘Timer1’ (for this example)
  2. Code an instance of your timer into a method within a module.

[code]Timer1= New Timer
Timer1.Period = 1000
Timer1.Mode = Timer.ModeMultiple

AddHandler WindowTimer.Action, AddressOf Timer1Action[/code]
3. Create a method called ‘Timer1Action’ (you can change this if you wish - provided that you change the ‘AddressOf Timer1Action’ also
4. Add the parameter as ‘sender as timer’.

Read this once you can comprehend how modules, classes and window instances work, etc. Norman explains this.
This might help you a lot.:
http://documentation.xojo.com/index.php/AddHandler

Thanks! It worked. I don’t know how I would have found out where to look and how to solve this, without the help of this forum… Again starting from the reference was no help and I had no clue what to search for except +xojo +module +timer…

Just one small mistake in the example, though (if anyone else needs the solution):

AddHandler WindowTimer.Action, AddressOf Timer1Action

Should be:

AddHandler Timer1.Action, AddressOf Timer1Action

You’ll need to add some sort of “initialization” to the module since they do NOT have constructors like classes do

Make sure you read the User’s Guide. The reference is written for people who already know the language.

[quote=140597:@Roger Jönsson]Thanks! It worked. I don’t know how I would have found out where to look and how to solve this, without the help of this forum… Again starting from the reference was no help and I had no clue what to search for except +xojo +module +timer…

Just one small mistake in the example, though (if anyone else needs the solution):

AddHandler WindowTimer.Action, AddressOf Timer1Action

Should be:

AddHandler Timer1.Action, AddressOf Timer1Action

No. This is not a mistake. You can name the method ‘Timer1Action’ whatever you wish and you can name the timer whatever you wish.

This is no error.

I copied literally took this code from the docs and changed WindowTimer to Timer1.

[quote=140626:@Oliver Scott-Brown]No. This is not a mistake. You can name the method ‘Timer1Action’ whatever you wish and you can name the timer whatever you wish.

This is no error.

I copied literally took this code from the docs and changed WindowTimer to Timer1.[/quote]

Actually, Oliver, your example does indeed have an error if you are trying to create a handler for Timer1. You wrote:

[code]Timer1= New Timer
Timer1.Period = 1000
Timer1.Mode = Timer.ModeMultiple

AddHandler WindowTimer.Action, AddressOf Timer1Action[/code]

That is incorrect. WindowTimer was not defined. Yes, you can name it whatever you want, but you added a handler to WindowTimer not Timer1.

Oh right. Sorry. My bad. I thought he was refering to the Xojo docs not my example.

Thanks

I actually did search the 4 chapters, but found nothing that would help med understand how to use a timer in a module.

You’l learn with experience and by asking other forum members.

Using a timer in a module is a bit specific. Did it cover delegates? You are meant to learn from it and use the knowledge flexibly.

Yes it is briefly mentioned in chapter 3, but it wouldn’t have helped with the questions I had…

It is true that I will learn with experience, but I have a hard time finding way in the documentation. There is probably some sort of Logic to it, that I still have to discover. If it is just me, but often when I try to lock something up, there are black holes (descriptions feels incomplete) and new questions are generated, by lack of info and not knowing what goes on, on the inside. Probably this gets easier with time, but I do feel that I lack a good solid reference.

[quote=140667:@Roger Jönsson]Yes it is briefly mentioned in chapter 3, but it wouldn’t have helped with the questions I had…

It is true that I will learn with experience, but I have a hard time finding way in the documentation. There is probably some sort of Logic to it, that I still have to discover. If it is just me, but often when I try to lock something up, there are black holes (descriptions feels incomplete) and new questions are generated, by lack of info and not knowing what goes on, on the inside. Probably this gets easier with time, but I do feel that I lack a good solid reference.[/quote]
I think the docs are an excellent reference. It has some confusing gaps that confuse me sometimes. I just ask my questions on the forums and get a quick answer.