# system.util.invokeLater question

**URL:** <https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954>\
**Category:** Ignition\
**Created:** [March 26, 2021, 11:40am UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954 "2021-03-26T11:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lachguerabdallah](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lachguerabdallah/32/25558_2.png) [@lachguerabdallah](https://forum.inductiveautomation.com/u/lachguerabdallah)\
**Post date:** [March 26, 2021, 11:40am UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954/1 "2021-03-26T11:40:26Z")

</div>

hello  
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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 26, 2021, 11:56am UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954/2 "2021-03-26T11:56:39Z")

</div>

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 event.source.data.getRowCount() > 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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 26, 2021, 12:56pm UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954/3 "2021-03-26T12:56:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 26, 2021, 1:03pm UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954/4 "2021-03-26T13:03:23Z")

</div>

> [@bkarabinchak.psi](#):
>
> 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.

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.

> [@bkarabinchak.psi](#):
>
> 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.

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.

> [@lachguerabdallah](#):
>
> I am using it in all my propertyChange scripts. is it good practice?

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.

> [@Loading bar during script execution](http://forum.inductiveautomation.com/t/loading-bar-during-script-execution/11520/2):
>
> In general, any script in a GUI should run in \< 0.1 second unless it’s put in the background. [system.util.invokeAsynchronous()](https://docs.inductiveautomation.com/display/DOC/system.util.invokeAsynchronous) is the tool you need to kick off your script. However, from the background, it isn’t safe to interact with the UI at all (Like, lock up or crash the whole client). So your script must collect it’s results and pass them to a script called from [system.util.invokeLater()](https://docs.inductiveautomation.com/display/DOC/system.util.invokeLater), which will run in the GUI thread and can place your results into the target components. For a progre…

---

<div class="post-metadata">

**Author:** ![lachguerabdallah](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lachguerabdallah/32/25558_2.png) [@lachguerabdallah](https://forum.inductiveautomation.com/u/lachguerabdallah)\
**Post date:** [March 26, 2021, 2:33pm UTC](https://forum.inductiveautomation.com/t/system-util-invokelater-question/44954/5 "2021-03-26T14:33:07Z")

</div>

thanks to all, now it is clearer to me.
