system.util.invokeLater question

I have a question about the system.util.invokeLater function.

I wanted to know what the function does exactly.
I am using it in all my propertyChange scripts. is it good practice?
I have read the documentation but it is not very clear to me.
Thank you

Basically it just delays when your function will be run on the GUI thread. So if you have some function foo() that runs on window startup and you call it with system.util.invokeLater(foo, 1000) in the windowOpened extension function, then 1000 ms after the window opens, it will try to run foo.

The reason the documentation says This is useful for events like propertyChange events, where the script is called before any bindings are evaluated. is that for certain propertyChange events, you probably don’t want to run them on the initial change/where the property is empty or not evaluated yet. So you tell your property change event to wait a little before executing to ensure you have data to work with.

Personally I try to find other ways as this is a bit race condition-y if you’re trying to guess how many ms you need. I normally append my conditions with things like and > 0 to ensure I am running the property change when I have data when applicable.

As for using it without any ms parameter, as in system.util.invokeLater(foo), I don’t know how that works under the hood and would like to hear from someone the logic that this sort of call follows.

1 Like

If milliseconds is omitted or is zero, the execution is simply queued up behind anything else already queued to run on the GUI thread. Good for cases where recursion might add more delay than desired.

1 Like

This is true, but not quite all the information. I would say, IMHO, you’ve left out the most important reason for the function.

invokeLater() waits until all of the processing and pending events on the event dispatch thread ('commonly referred to as the GUI thread) are done being processed, then invokes the supplied function on the GUI thread.

This leads to two important uses of the function

  1. As described it is useful for propertyChange events where you want to wait for the bindings to be evaluated.

  2. In an asynchronous thread this function is used to move any UI interaction back to the Event Dispatch Thread.

If the delay parameter is supplied, then the supplied function will be invoked after all currently pending events are processed plus the delay, if no delay parameter is supplied then the function is invoked immediately after all currently pending events are processed.

I’m not sure I would label it as good or bad practice, only ‘probably unneeded’.

Here is a good post on the topic, it’s a little dated but still very applicable.


thanks to all, now it is clearer to me.