# RefreshBinding after a delay

**URL:** <https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253>\
**Category:** Ignition\
**Tags:** perspective, ignition81, scripting, python\
**Created:** [March 17, 2023, 5:42pm UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253 "2023-03-17T17:42:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [March 17, 2023, 5:42pm UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/1 "2023-03-17T17:42:03Z")

</div>

Hi,

I was wondering if it's possible to trigger a RefreshBinding after a delay in a component.

For example, this a one shot button:

```auto
from threading import Timer
	
	InsertData()
	
	def runThisLater(param):
		system.perspective.sendMessage("Table Refresh Data", payload = {}, scope = "page")
	
	Timer(2.0, runThisLater, ["Stop Waiting, I'm done"]).start()
	
	self.props.value = 0

```

So, InsertData() will add some entries to a database and after that's done I would like to refresh the named query binding on a table.

The message handler script just have one line:

```auto
self.refreshBinding("props.data")

```

If a run a SendMessage outside a delay it would refresh, but not inside.

What could trigger a RefreshBinding after "whatever is being done with the database" is completed?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 17, 2023, 6:49pm UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/2 "2023-03-17T18:49:19Z")

</div>

I fail to see the need for a delay here. If you write your code in a blocking manner, then you can simply refresh the binding after the insert data function; otherwise, how would you know how long to wait before you refresh the binding?

```python
InsertData()
self.getSibling("Table").refreshBinding("props.data")

```

---

<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 18, 2023, 8:24am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/3 "2023-03-18T08:24:37Z")

</div>

> [@dkhayes117](#):
>
> simply refresh the binding after the insert data function

This.

And ditch all use of `Timer` (or any purely python infrastructure--use java instead).

---

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [March 20, 2023, 9:35am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/4 "2023-03-20T09:35:01Z")

</div>

From my tests, it doesn't update to the new values right away.

Another example would be:

1. Insert data in a table.
2. Read inserted data.
3. Result: Old data or an error if the table was empty.

So, it does take a couple of milliseconds to get to the database.

> [@dkhayes117](#):
>
> how would you know how long to wait before you refresh the binding?

That's a good point. If the database is down then I'll just read old data in the case of inserting entries and in the case of showing data with a RefreshBinding nothing will be shown.

> [@pturmel](#):
>
> And ditch all use of `Timer` (or any purely python infrastructure--use java instead).

The function `system.util.invokeLater(function, [delay])` can only be used for Vision. Apart from `from threading import Timer`, I don't see anymore options in the manual.

Gateway Event timers, as far as I know, cannot be controlled using a start/stop function.

While answering this thread I came across with `java.util.Timer.scheduleAtFixedRate()` from [https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-TimerScriptSettings](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-TimerScriptSettings)  
Are you referring to those functions?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 20, 2023, 10:15am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/5 "2023-03-20T10:15:34Z")

</div>

What Phil meant is that you can import Java into Jython, which means you can use

```python
from java.util import Timer

```

instead of the python version.  
And he suggests you do.

---

<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 20, 2023, 10:39am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/6 "2023-03-20T10:39:13Z")

</div>

> [@pascal.fragnoud](#):
>
> he suggests you do.

If you absolutely must. The correct answer is to arrange your code and the operations it performs so that all waiting is naturally part of the operations, and therefore no sleeping or timing is required. A scripted DB update followed by a binding refresh fits this pattern. As @dkhayes117 points out above.

Using any form of `.sleep()` is a "code smell" that suggests you are doing something wrong or architecturally unwise.

You say that the new data is not in place for Select immediately after your Insert operation. That is a DB bug or config error--quite likely poor choice of transaction isolation. Fix **that**.

Perhaps you should show your code for `InsertData()`.

---

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [March 20, 2023, 11:25am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/7 "2023-03-20T11:25:19Z")

</div>

> [@pascal.fragnoud](#):
>
> What Phil meant is that you can import Java into Jython, which means you can use

Sounds good. I wonder why it's not in the manual.

@pturmel  
I agree with everything from your last post.  
This is the part of the code where I could see that I cannot just insert and right after that read the DB.

```auto
def InsertDate():
	
	date = system.date.now()
	
	system.db.runSFPrepUpdate('INSERT INTO time (storetime) VALUES (?)', [date], datasources=["TestDB"])
	
	return None

def InsertData():
	
	InsertDate()
    
    timeID = system.db.runScalarQuery("SELECT max(id) FROM time", "TestDB")
	
	return None

```

When working on the tables, I also noticed that I cannot just delete and refresh the table in the very next instruction. So, something needs to guarantee that the DB operation is done before moving forward. That's the poor architecture that I have right now.

---

<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 20, 2023, 11:37am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/8 "2023-03-20T11:37:10Z")

</div>

> [@tupanaster](#):
>
> `system.db.runSFPrepUpdate`

This is the problem. Don't use the store and forward system for user-interface operations.

---

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [March 20, 2023, 11:40am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/9 "2023-03-20T11:40:03Z")

</div>

Part of the code is inserting with a button and the other part will run automatically on a scheduled basis. I would think that in both scenarios it should not use the store and forward system. Is that correct?

---

<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 20, 2023, 11:41am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/10 "2023-03-20T11:41:23Z")

</div>

Definitely not for the button. The automatic part might, depending on whether you are handling errors or just want the insert to wait until the DB can accept it.

---

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [March 20, 2023, 11:52am UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/11 "2023-03-20T11:52:53Z")

</div>

> [@pturmel](#):
>
> Definitely not for the button. The automatic part might, depending on whether you are handling errors or just want the insert to wait until the DB can accept it.

I think your answer is the solution to the thread but I'll take it a bit further.

I should do some error handling. Writing directly to the database and wishing for the best in the background doesn't seem correct. Making sure that the data is written should be a customized solution or is there a standard way that I should look at?

---

<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 20, 2023, 12:15pm UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/12 "2023-03-20T12:15:21Z")

</div>

Catching errors with database calls generally requires explicitly capturing java exceptions in an `except:` clause separate from jython errors. Start here:

[https://forum.inductiveautomation.com/search?q=except%20Throwable%20%40pturmel%20order%3Alatest](https://forum.inductiveautomation.com/search?q=except%20Throwable%20%40pturmel%20order%3Alatest)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 20, 2023, 1:12pm UTC](https://forum.inductiveautomation.com/t/refreshbinding-after-a-delay/71253/13 "2023-03-20T13:12:52Z")

</div>

> [@tupanaster](#):
>
> I wonder why it's not in the manual.

Because it's a Jython thing, not specific to ignition.
