# Update progress bar from within a script

**URL:** <https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995>\
**Category:** Ignition\
**Created:** [January 19, 2019, 4:45pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995 "2019-01-19T16:45:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curlyandshemp](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@Curlyandshemp](https://forum.inductiveautomation.com/u/Curlyandshemp)\
**Post date:** [January 19, 2019, 4:45pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/1 "2019-01-19T16:45:51Z")

</div>

I have this script on a button to copy data to a PLC from a database. Since this script takes 10 - 20 seconds to run, i would like to have the progress bar show progress. Issue I have is the progress bar updates only when the script has completed. Is there anyway to get value outside of the script to update as the script is running?

 ![2019-01-19%2011_41_44-](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2de126ccc6ad13afd334bd35576dc9d53b13e4f5.png)

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [January 19, 2019, 5:08pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/2 "2019-01-19T17:08:26Z")

</div>

> [@Progress Bar](http://forum.inductiveautomation.com/t/progress-bar/1130/2):
>
> Sure, you can have a function that you run using invokeAsynchronous, and pass to it a reference to a progress bar component. Then use invokeLater’s to update progress bar (updates to the progress bar shouldn’t come directly from a background thread - invokeLater’s will get processed on the GUI thead)

---

<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:** [January 19, 2019, 6:24pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/3 "2019-01-19T18:24:38Z")

</div>

Event scripts run in the foreground. Painting the UI runs in the foreground, so it can't happen until your script finishes. You need to wrap up your long-running operation into a function, and then run that with invokeAsynchronous. That function can send updates back to the foreground with invokeLater.

You _must_ not _ **ever** _ assign to component properties or call component methods from an asynchronous (background) function. You can really scramble Swing and/or deadlock it. Take a look at this thread for a discussion of possibilities:

> [@Update GUI via Asynchronous thread](http://forum.inductiveautomation.com/t/update-gui-via-asynchronous-thread/9666):
>
> I’ve got a script I am experimenting with that can take a lot of time to execute, so I now use the system.util.invokeAsynchronous to run the script in a separate thread to keep the GUI from locking up. I am using the asynchronous thread to copy files from one place to another, and I would like to show the progress of the file copy with a progress bar. How would I go about this? I am importing a few python libraries to do this. import shutil def fileTransfer(): path = system.file.openFile() …

Naturally, I recommend using the `callAsync()` and `assignLater()` functions from my `later.py` script module.

---

<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:** [January 19, 2019, 6:36pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/4 "2019-01-19T18:36:29Z")

</div>

A long-standing general rule of thumb, applicable to every GUI programming language I’ve ever used, is that foreground event scripts should never run longer that 1/10 of a second. Anything longer should be asynchronous. That would include any system.\* call that might need to request something from the gateway. If you let events run longer, you will have user-notice-able UI freezes.

---

<div class="post-metadata">

**Author:** ![greg.krueger](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@greg.krueger](https://forum.inductiveautomation.com/u/greg.krueger)\
**Post date:** [August 5, 2020, 3:41pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/5 "2020-08-05T15:41:33Z")

</div>

Resurrecting a bit of an old thread here, but am trying to use Async and InvokeLater functions and had a question on using them.

1. 
  - For InvokeLater, how does that work if the user navigates away from the display that the InvokeLater is trying to do an update to (ie a progress bar or status label.text property). Does it just drop the commands since the referenced objects are no longer on the active display? Our navigation is via a tab strip header with Swap window option.

Here’s my code snippet. For this application, if the user navigates away and the script doesn’t finish, it’s fine, this is an on-demand batch report generation display, if they don’t wait for the report to finish, they just would need to come back and try again and be more patient! But ideally if they navigate away, I don’t want any errors popping up on the GUI when the async finishes in the background.

```auto
lblGenerationStatus = event.source.parent.getComponent('lblGenerationStatus')
lblGenerationStatus.text = "Generating Report"
lblHiddenFilename = event.source.parent.getComponent('lblHiddenFilename')

def batchGen():
	(strFilename, strStatus) = WM_Reports.GenerateBatchReport(batchParams)

	def UIFunc():    	
		lblGenerationStatus.text = strStatus
		lblHiddenFilename.text = strFilename
	system.util.invokeLater(UIFunc)
system.util.invokeAsynchronous(batchGen)

```

---

<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:** [August 5, 2020, 3:47pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/7 "2020-08-05T15:47:43Z")

</div>

When a Swing object is held in a variable in a background or other task, and the Swing object closes or is otherwise deleted from the display, it continues to exist “disconnected” from the UI until all references are gone. In your case the component’s text properties will be updated, invisibly, just before the components themselves are discarded. If enough time has passed by the time they execute, those component’s parent properties would be null and other bindings/event routines discarded.

---

<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:** [August 5, 2020, 3:49pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/8 "2020-08-05T15:49:58Z")

</div>

> [@greg.krueger](#):
>
> didn’t come out properly

You can edit your posts. And you need to mark your code sections so they format properly (with indents). Use triple back-quotes above and below your code. It should look like so in the post editor:

```  
Pasted code or other plain text (logs...)  
```

---

<div class="post-metadata">

**Author:** ![greg.krueger](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@greg.krueger](https://forum.inductiveautomation.com/u/greg.krueger)\
**Post date:** [August 5, 2020, 4:18pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/9 "2020-08-05T16:18:51Z")

</div>

Phil,

Thanks for the feedback, on the code (and on posting).

So it sounds like I shouldn’t get any errors after navigating away, at least any due to the Async/InvokeLater functions. While testing my display, I was pretty sure I was getting some errors when I navigated away but not when I stayed on the display, but I haven’t been able to reproduce them, so might have been unrelated to the Async and a typo in a reference.

---

<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:** [August 5, 2020, 4:22pm UTC](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995/10 "2020-08-05T16:22:43Z")

</div>

> [@greg.krueger](#):
>
> I was pretty sure I was getting some errors

Well, any components passed to the async script will be present, but if you try to look up other components in the later script, they probably won't be there. That can generate errors. `try...except...` is your friend. (:
