# Opening popup and sending message request at same time

**URL:** <https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 4, 2024, 10:44pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819 "2024-07-04T22:44:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [July 4, 2024, 10:44pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/1 "2024-07-04T22:44:52Z")

</div>

I'm opening a popup with `system.perspective.openPopup` and then trying to populate some values in the popup / do some things by sending a message with `system.perspective.sendMessage` .

I'm running into the obvious timing issue that the popup View might not quite be loaded up and ready to receive via the message handlers at the time the calling function broadcasts.  
This could be solved by adding a small time delay between the opening of the popup and the sending of the message, with something like `time.sleep(0.1)`, but it got me thinking about what an **appropriate** or **minimum** time delay would be...

Is there any clever way to have an asynchronous call that lets the calling script know that the openPopup function has been completed and the View is ready to go?  
I don't really want to rely on a timer if we don't have to..

---

<div class="post-metadata">

**Author:** ![simeonw](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@simeonw](https://forum.inductiveautomation.com/u/simeonw)\
**Post date:** [July 4, 2024, 10:58pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/2 "2024-07-04T22:58:35Z")

</div>

If you need to pass values to a popup, the parameters (using the **params** prop) are usually the best and easiest way to do this:

> **[system.perspective.openPopup | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-openPopup)**
>
> Open a popup view over the given page.

Messages also work, but as you can appreciate, they introduce added complexity. Timers can also cause issues, unless they are run on a different thread:

> **[system.util.invokeAsynchronous | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/7.9/appendix/scripting-functions/system-util/system-util-invokeAsynchronous)**
>
> This is an advanced scripting function. Invokes (calls) the given Python function on a different thread.

If you still wanted to do a message handler, you could have the popup send a message to the view once it has loaded and then page send a message back with the data.

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [July 4, 2024, 11:12pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/3 "2024-07-04T23:12:34Z")

</div>

Thanks, yea I did start with sending a parameter through in the openPopup call, but this is for a Trend popup where we wanted to be able to add multiple tag pens to it via some other mechanism.  
The parameter was ok for adding an initial pen by sending through a tagPath parameter, but adding additional pens we used the message handler.

I was hoping to simplify the work flow a bit by **not** using the parameter for the initial call, so that it is consistent.

---

<div class="post-metadata">

**Author:** ![simeonw](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@simeonw](https://forum.inductiveautomation.com/u/simeonw)\
**Post date:** [July 4, 2024, 11:21pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/4 "2024-07-04T23:21:46Z")

</div>

If it is a popup that multiple views are modifying, you could use store the pens in a custom session prop and both the views and popup can read/write pens?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 5, 2024, 11:20pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/5 "2024-07-05T23:20:21Z")

</div>

Ha, I did the same thing recently and ran into the same problem. I don't like my solution.. But it works.. But like you, I wasn't sure how to get around it.

I had the msg handler in the popup which adds the tags to the trend.  
I added params to the popup to pass a list of tag paths into. I added an on value change script handler on the tag list to send it to the message handler which then adds the tags to the trend.  
The button to open the popup sends the tags via params. Importantly, the openPopup call is discarded automatically when the popup isn't already open (can't reopen a popup with same id), so the params are only ever sent once when the popup is first opened.  
The button _also_ sends a message to the message handler to add the tags as well, with no delays or anything.

This works because if the popup isn't open, the params add the trend tags and the message sent from the button doesnt make it to the popup because it's too slow to open. (This was very consistently the case in my testing)

If the popup is already open, the params don't make it into the popup, but this time the message does make it to the popup and the tags are added that way...

It feels gross and it is, but I'm not sure what else to do

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 5, 2024, 11:27pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/6 "2024-07-05T23:27:53Z")

</div>

> [@simeonw](#):
>
> unless they are run on a different thread:

I quote Paul the Wise:

> [@Can we use system.util.invokeAsynchronous in Ignition Perspective to call custom functions in components?](https://forum.inductiveautomation.com/t/can-we-use-system-util-invokeasynchronous-in-ignition-perspective-to-call-custom-functions-in-components/76282/3):
>
> You absolutely _can_ invoke async threads. It's a relatively heavyweight operation though, and I would not recommend it just for slightly better developer ergonomics. Embrace message handlers so that work happens on existing Perspective worker threads as much as possible.

---

<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:** [July 5, 2024, 11:29pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/7 "2024-07-05T23:29:51Z")

</div>

> [@nminchin](#):
>
> This works because if the popup isn't open, the params add the trend tags and the message sent from the button doesnt make it to the popup because it's too slow to open. (This was very consistently the case in my testing)

Consider sending the message just **before** opening the Popup with parameters. That will ensure there's no race condition at all.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 5, 2024, 11:34pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/8 "2024-07-05T23:34:45Z")

</div>

Haha, I realised that actually after I typed it, I was going to fix it up when on the laptop as it was already a long non-tactile-feedback run 😅

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [July 7, 2024, 10:02pm UTC](https://forum.inductiveautomation.com/t/opening-popup-and-sending-message-request-at-same-time/92819/9 "2024-07-07T22:02:07Z")

</div>

Thanks Nick,

Didn't want to start with it, but we've actually done this exact method initially, was trying to see if there was alternative ways 😃  
But seems like the best way we've got for now...
