Loading symbol while waiting for script to complete

I have a button that when pressed, runs a script that in turn runs a bunch of SQL SELECT/UPDATE statements, and then closes the window. This takes a few seconds to run, and while it is running, the entire ignition window is unresponsive.

Is there a way to make a loading image or something? So that the user doesn’t feel that the client has crashed?

All scripting you do in the client runs (by default, and very deliberately) on the main Java Swing thread (also known as the Event Dispatch Thread). Any interaction with the GUI (ie, telling a component property to update, or animating a progress bar) must happen on this thread, or deadlocks can result.

We offer two helper functions to aid in working around this restriction:
system.util.invokeAsynchronous which allows you to run a defined function on a background thread, and
system.util.invokeLater, which allows that background thread to place operations into the queue of the GUI thread to maintain thread-safety. So an extremely reductive example would be something like:

def setProgressBarState(target=event.source.parent.getComponent('Progress Bar')):
	target.value = target.value + 10

def mySlowFunction():
	# run some slow DB operation
	system.util.invokeLater(setProgressBarState)
	# run another slow DB operation
	system.util.invokeLater(setProgressBarState)
	# run another slow DB operation
	system.util.invokeLater(setProgressBarState)

system.util.invokeAsynchronous(mySlowFunction)
1 Like