IMDTouchKnowledge BaseShop main store

Troubleshooting

Why an HMI Screen Freezes Until a Script Finishes

HMI and touchscreens Published 6 min read

Short answer

The event handler runs on the single interface thread that also paints the screen, so repaints queue behind it and the status text appears only when the handler exits. Keep handlers to about 100 ms, run RFID, tag and SQL work through system.util.invokeAsynchronous(), and post display changes back with system.util.invokeLater().

On this page
  1. One thread runs both the script and the paint
  2. First check whether the value changes at all
  3. Filter the event before you dispatch anything
  4. Move the long work with system.util.invokeAsynchronous
  5. Why a delayed invokeLater is not a fix
  6. Scheduling choices at a glance
  7. Worked example: 1320 ms against the guideline
  8. The fix, step by step
  9. Questions asked

Your script sets the status text to 'SCRIPT RUNNING' in its first line, and the operator sees nothing until the handler exits — then both texts paint in one collapse, or only the last one does. Nothing is wrong with the display: the event handler runs on the same single interface thread that paints the window, so every repaint queues behind it. Keep event handlers to roughly 100 ms of work; run the RFID evaluation, SQL and tag waits through system.util.invokeAsynchronous(); post display changes back with system.util.invokeLater(). A delayed call such as system.util.invokeLater(evaluateRFID, 2500) is still the interface thread — a delay, not a fix.

One thread runs both the script and the paint #

The window toolkit behind an Ignition Vision client is single-threaded: one interface thread pumps input events, runs event handlers, and paints components. A property assignment and a repaint are separate operations — writing 'SCRIPT RUNNING' to a text property requests a paint, it does not perform one. While your handler runs, that thread is busy and the paint request waits in the queue. The toolkit also collapses repeated paint requests for the same component into a single paint, so two writes in quick succession can render as one: the last value. That is the whole symptom — by the time the thread is free, the property already holds 'SCRIPT STOPPED', and the queued request paints that.

Hold a handler under 100 ms of work. That is a design guideline, not a runtime limit the software enforces: below it an operator perceives the response as immediate, and past it paint latency becomes visible while queries and tag waits stretch the freeze to whole seconds. Anything slower belongs on a background thread.

First check whether the value changes at all #

Before touching threading, read the status value at the moment the handler assigns it and compare that with what the operator sees.

  • The value changes but paints late: event-thread occupancy. Continue below.
  • The value never changes: the assignment path is wrong — wrong destination, a later overwrite in the same handler, or a binding driving the same property. Fix that first; threading will not help.
  • The value and its quality are good, but the whole screen lags even with no script running: the communication path, not the handler. Start with why an HMI freezes or updates slowly over serial.

A screen that stays frozen during a SQL query is not evidence of a controller or driver fault; the controller may be serving the tag fine while the interface thread never gets around to painting it.

Filter the event before you dispatch anything #

A scan into a password text box raises a propertyChange event for more changes than the operator counts as one scan — focus, selection, interim edits. Read which property changed and its new value from the event object, keep only the change you evaluate, and capture the scan text plus anything else the worker needs into local variables before you dispatch. The worker must never reach back for the text box: the operator can clear it or scan again while the work runs.

Decide what a second scan does while work is active — reject it, queue it, or let it run independently — and make one rule own the status text, so a completion posted by an earlier scan cannot overwrite the status of a later one. Frequent events multiply the occupancy: the same single thread does the work when an HMI script counts PLC bit changes on every transition, so filter hard and keep each pass short.

Move the long work with system.util.invokeAsynchronous #

system.util.invokeAsynchronous() invokes the given function on a different thread and returns immediately, so your handler exits, the interface thread drains its queue, and the status paints while the work proceeds. Each call creates one new short-lived thread. The rule that comes with it: background threads may not interact with windows, components, or their properties at all. Write results to tags, or post them back with system.util.invokeLater(), whose callback runs on the interface thread after current processing and pending events finish.

# Illustrative names: rfidBox, statusText, resultText and lookUp are yours;
# the two system.util calls are the documented threading functions.
scanValue = rfidBox.text              # str; captured on the interface thread
statusText.text = 'SCRIPT RUNNING'    # property write on the interface thread

def evaluateScan(value):              # runs on the new background thread
    record = lookUp(value)            # SQL and tag work; no component access

    def showResult():                 # posted back to the interface thread
        statusText.text = 'SCRIPT STOPPED'
        resultText.text = record

    system.util.invokeLater(showResult)

system.util.invokeAsynchronous(lambda: evaluateScan(scanValue))

The test that proves it: record the clock at handler entry and at the dispatch line, and the difference is your interface-thread occupancy. Scan once — 'SCRIPT RUNNING' must be visible while lookUp is still running. Scan twice in quick succession — the second scan must follow the reject-or-queue rule you chose. A project script is a fine home for evaluateScan: a script module organizes code, it does not control threading, and the same function can serve a foreground event or a background dispatch.

Why a delayed invokeLater is not a fix #

system.util.invokeLater(evaluateRFID, 2500) does not create a worker. The 2500 is a delay argument, in milliseconds; when it expires, evaluateRFID still runs on the interface thread, and its tag reads, tag writes and SQL queries occupy it again. The call returns immediately and the gap before the delayed start can let a paint through, which is why it looks like progress at first. Then the freeze returns, shifted later in time, which reads as an intermittent fault. Use the delay for short interface actions that must happen later, never as a substitute for background execution. Where no client interface exists at all, how invokeLater provides delayed script execution on the gateway is a separate, legitimate pattern.

Scheduling choices at a glance #

Call Where it runs Right for
Direct code in the event handler Interface thread, immediately Filtering the event, capturing inputs, quick property writes
system.util.invokeLater(fn) Interface thread, after pending events Brief component and window updates
system.util.invokeLater(fn, 2500) Interface thread, after the delay Short delayed interface actions — not long work
system.util.invokeAsynchronous(fn) A new background thread RFID evaluation, SQL, tag waits — no component access

Worked example: 1320 ms against the guideline #

Take a handler that runs three SQL queries at 400 ms each and two controller tag transactions at 60 ms each. Those two figures are example inputs — measure your own query and tag times and substitute them; the arithmetic and the decision do not change.

  • 3 x 400 ms = 1200 ms
  • 2 x 60 ms = 120 ms
  • 1200 ms + 120 ms = 1320 ms of interface-thread occupancy per scan
  • 1320 ms / 100 ms = 13.2 times the guideline

Restructured, the handler filters the event, captures the scan text, writes the status and dispatches — call it 1 ms each, 4 ms total. The interface thread is occupied for 4 ms, the 1320 ms of work runs beside it, and the status paints immediately.

The decision, case by case:

  • A handler that can exceed 100 ms, or that waits on SQL, tags or an RFID evaluation: move it to system.util.invokeAsynchronous().
  • Every display change from the background thread: post it with system.util.invokeLater(), or write a tag the display binds to.
  • A delayed invokeLater: only for short interface actions that genuinely must wait.
  • A handler that only filters, captures and writes one or two properties: leave it alone.

The fix, step by step #

  1. Reproduce the freeze and time the handler from entry to exit.
  2. Read the status value at the assignment point and pick the branch above: assignment fault, comms lag, or event-thread occupancy.
  3. List every slow operation in the handler and in every helper it calls — queries, tag waits, deliberate sleeps.
  4. Filter the propertyChange event to the one change you evaluate, and capture the scan text into a local variable.
  5. Move the long work into a function that touches no window, component or property; keep it in a project script if that helps reuse.
  6. Write 'SCRIPT RUNNING' on the interface thread, then dispatch with system.util.invokeAsynchronous().
  7. Post the completion and every display update back with system.util.invokeLater().
  8. Test with one scan, then two in quick succession, and re-time the handler.

Keep safety functions in the controller: neither the interface thread nor a background script thread is the place for an interlock or an e-stop chain.

Questions asked

Why does SCRIPT RUNNING flash only after the script ends?

Both writes complete before the interface thread gets paint time, so the queued paint of the first value lands moments before the second paints, and that is the flash. When the two writes land close together, the toolkit collapses the repeated paint requests into one and even the flash disappears: the operator sees only the final text.

Does system.util.invokeLater(evaluateRFID, 2500) run in the background?

No. The 2500 is a delay before the callback runs, and the callback still executes on the interface thread, where its queries and tag writes block painting exactly as before. The delay only moves the start of the freeze about 2.5 seconds later, which often reads as an intermittent fault instead of a blocked one.

Can the background thread update the display by writing a tag instead?

Yes. Tag writes are permitted from the background thread. If the status text is bound to that tag, the binding carries the new value to the component and the worker never touches a component at all. That route is often simpler when several windows must show the same status.

Is a project script the same as a background thread?

No. A script module organizes code; it does not decide where the code runs. The same project function can be called directly from an event or dispatched asynchronously. Many projects add thin wrapper functions in a shared script so the capture-and-post pattern is written once and reused everywhere.

How do I measure whether a handler is short enough?

Record the clock at handler entry and again at the dispatch line; the difference is the interface-thread occupancy, and it should sit under the 100 ms guideline. Time any invokeLater callback separately, because a short dispatcher can hide a long callback that still blocks painting. Time the background worker on its own to size the operator's wait.

Keep reading

  • How-to

    Building a Pie Chart on a Comfort Panel in TIA Portal

    A TP1200 Comfort project in TIA Portal V17 has no pie chart object, and its scripts cannot draw one. Compute each sector in a local script as 360 degrees times value divided by total, guard the zero-total and invalid-data cases before dividing, then display the result by switching pre-drawn chart images from an integer tag or scaling bar objects to the percentages.

    HMI and touchscreens

  • How-to

    Keep a PanelView Plus Trend Logging After Screen Changes

    A PanelView Plus Trend object only records while its screen is visible. Configure a Data Log model to start at power-up, show it through an XY trend with pen 2 as the X axis, and give the log a bounded record limit so it overwrites the oldest records instead of growing without bound.

    HMI and touchscreens

  • How-to

    Verifying HMI Historical Trend Retention Before Committing

    An 8-hour trend sampled every 5 minutes spans 96 intervals — 97 stored points if both endpoints are kept — so the panel needs a historical buffer of at least 97 samples per tag that repopulates the trend after screen changes. Verify the buffer depth, the interval setting, and restoration behavior on the bench before you build the screens.

    HMI and touchscreens

Display a Previous-Day Historian Average on an HMI Screen