[SOLVED] Update HMI Screen while script is running

Good afternoon,

I have a script that runs when the user scans an RFID. What I have noticed is the screen will not update until the script has finished running. So if I change some text on the screen to “SCRIPT RUNNING” at the beginning of my script and “SCRIPT STOPPED” at the end of my script I will not see any change on the screen until the script has finished running at which point I might briefly see “SCRIPT RUNNING” which immediately changes back to “SCRIPT STOPPED”.

Any clue on how to make the “SCRIPT RUNNING” appear as soon as I write that value in the script?

That is what system.util.invokeAsynchronous() is for. Event scripts should never run for more than a tiny fraction of a second, as the java Swing user interface is single-threaded. 100 milliseconds is a good rule of thumb. Anything longer should be put on a background thread, which can then post its results using system.util.invokeLater(). Note, background threads may not interact with windows, components, or their properties at all.

There are numerous discussions of invokeAsynchronous and invokeLater in this forum’s archives.

2 Likes

That is what system.util.invokeAsynchronous() is for. Event scripts should never run for more than a tiny fraction of a second, as the java Swing user interface is single-threaded. 100 milliseconds is a good rule of thumb. Anything longer should be put on a background thread, which can then post its results using system.util.invokeLater(). Note, background threads may not interact with windows, components, or their properties at all .

This is very interesting news to me. I have a password text box on my screen. The user scans an RFID tag on an RFID reader and a 'propertyChange' event is triggered. I do some checking to ensure this propertyChange is the one I want and then execute the following:

system.util.invokeLater(evaluateRFID, 2500)

And within the same propertyChange script I go off to the evaluateRFID definition. This definition reads/writes to tags and does some SQL queries but it doesn't appear to interact with the window, components, or their properties. Could I, or should I, move evaluateRFID to a project script location?

Well, don't confuse background with project script. A project script can be used in both foreground (events) and background. Script modules are a way to avoid repeating oneself in code--not a control over how they are executed. Based on your description, I would certainly move that task to a function run asynchronously, and a project script is a good place to put such a function.

This sort of stuff is enough of a minefield that I created a number of wrapper functions to help, to be installed as a shared or global script. You might find this topic helpful:

4 Likes

Just wanted you to know these wrappers saved my life. They are awesome!

1 Like