Timer question

I have a TextField and a Listbox on a window.

The Window has a Search method

Public Sub Search()
    Listbox1.AddRow TextField1.Text
  End Sub

and in the TextField’s KeyDown event I have

    Function KeyDown(Key As String) Handles KeyDown as Boolean  
      Xojo.Core.Timer.CancelCall(AddressOf Search)
      Xojo.Core.Timer.CallLater(500, AddressOf Search)
    End Function

that adds the content of the TextField to the ListBox when I stopp typing.

Works beautiful - but why is it working at all? I haven’t defined a timer anywhere.

And what would be the best way to deal with having several TextFields where the ListBox gets updated when each TextField contains text and when I stopp typing (with the order of text entry into the TextField being up to the user)?

Note that Cancel Call and CallLater are shared methods :slight_smile:
Basically they create a timer if one does not exist that is used to schedule the call
And if there is nothing scheduled then the cancel fails to find that delegate and does nothing

The second part I need to go eat and will replay later when I’m done :stuck_out_tongue:

1 Like

So if I get that right: if I would use that code within the timer period somewhere else it would stop that timer and restart it without adding to the ListBox?

That’s quite a caveat …

Not sure I follow ?
If you are already running the action it wont cancel the currently running action

And what would be the best way to deal with having several TextFields where the ListBox gets updated when each TextField contains text and when I stopp typing (with the order of text entry into the TextField being up to the user)?

you could have each text field update the listbox using similar code
I cant think you’d have contention issues BUT its possible I suppose
A critical section may be needed but start without one

Let me clarify that:

if you have a timer somewhere else (let’s say one that fires every 10 min) would that one not also be stopped and restarted - now with a 500 ms period?

No, doesn’t seem to be the case. But why not?

Because its not the same timer instance
CallLater schedules its own timer(s) internally and any others you create are independent of that

1 Like

Thanks - that explains it then :slight_smile:

One more: what exactly is the difference between Timer and Xojo.Core.Timer? Only the later seems to have the shared methods …

Internally ? not a ton as far as I remember
Really its just how its exposed to the end user

1 Like