# Tag.read after tag.write

**URL:** <https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218>\
**Category:** General Discussion\
**Created:** [February 16, 2016, 8:39am UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218 "2016-02-16T08:39:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgmo](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@pgmo](https://forum.inductiveautomation.com/u/pgmo)\
**Post date:** [February 16, 2016, 8:39am UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/1 "2016-02-16T08:39:31Z")

</div>

Hi,  
I have a question about system.tag.read and system.tag.write instructions: if I execute a read just after a write on the same tag path, e.g.

`system.tag.write(tagPath,someValue)
newValue=system.tag.read(tagPath).value`  
shall the newValue be the value just written to tagPath by the write instruction? Or is the result not deterministic: it could be the old value, i.e. the value stored in tagPath before the write instruction, as well as the new one, i.e. the one written to the tagPath with the write instruction, it depends on network speed, CPU load, Gateway load, etc?

Thanks in advance, regards

Edit  
Just found the answer in the help: system.tag.writeSynchronous does what I need, i.e. write and wait for the value to be stored in the PLC.

---

<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:** [February 16, 2016, 1:25pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/2 "2016-02-16T13:25:32Z")

</div>

[quote=“pgmo”]Edit  
Just found the answer in the help: system.tag.writeSynchronous does what I need, i.e. write and wait for the value to be stored in the PLC.[/quote]Yes, but remember that writeSynchronous() cannot be used in the foreground, that is, it cannot be used directly in event scripts. You may only use it in a thread spawned by [system.util.invokeAsynchronous()](https://docs.inductiveautomation.com/display/DOC/system.util.invokeAsynchronous).

---

<div class="post-metadata">

**Author:** ![pgmo](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@pgmo](https://forum.inductiveautomation.com/u/pgmo)\
**Post date:** [February 16, 2016, 2:27pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/3 "2016-02-16T14:27:13Z")

</div>

> [@](#):
>
> remember that writeSynchronous() cannot be used in the foreground, that is, it cannot be used directly in event scripts

Thanks pturmel, you are right.

---

<div class="post-metadata">

**Author:** ![john.furby](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john.furby](https://forum.inductiveautomation.com/u/john.furby)\
**Post date:** [April 4, 2020, 11:10pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/4 "2020-04-04T23:10:21Z")

</div>

So I am struggling with this as well.I write to a dataset tag via a save button. I see the values change in the tag browser, but when I do a tag read too fast but still after I see the change, it doesn’t read the new data. I have to wait about 3 seconds or more.

These are both buttons, so it doesn’t seem that Synchronous is an option for me. Why does it not read the new data as soon as I can see it in the tag browser? How can I get an indication that the data is ready to be read?

Thanks.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 4, 2020, 11:30pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/5 "2020-04-04T23:30:03Z")

</div>

You’ll have to do something annoying like use system.util.invokeAsync to invoke a function that does a synchronous write and read on a different thread. Then finish whatever logic your button press is doing there, making sure that if you interact with the UI you do it back on the UI thread using system.util.invokeLater…

---

<div class="post-metadata">

**Author:** ![john.furby](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john.furby](https://forum.inductiveautomation.com/u/john.furby)\
**Post date:** [April 4, 2020, 11:50pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/6 "2020-04-04T23:50:31Z")

</div>

So system.util.invokeAsync to do work in another thread, then system.util.invokeLater to toggle back to normal thread? Is this how you’d write code that might take a minute to load and you don’t want the UI to show frozen?

Thank you

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 4, 2020, 11:56pm UTC](https://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218/7 "2020-04-04T23:56:21Z")

</div>

> [@john.furby](#):
>
> Is this how you’d write code that might take a minute to load and you don’t want the UI to show frozen?

Yes, exactly.

And preventing a frozen UI is why writeSynchronous throws an error when you try to use it from the UI thread.
