How to Close a Perspective Popup Asynchronously After a Delay in Ignition?

Hello everyone,

I am working on an Ignition project using Perspective, and I want to create a toast notification using a popup. The popup should automatically close after a certain delay (e.g., 5 seconds) without blocking the main thread. Here's what I've tried so far:

  1. Opening the Popup: I'm using system.perspective.openPopup, and that works perfectly.
  2. Closing the Popup After Delay
  • tried using system.util.invokeAsynchronous to create a separate thread and used time.sleep to add a delay.
  • After the delay, I called system.perspective.closePopup to close the popup.

However, this doesn't seem to work as expected, but the closePopup function does not execute or fails silently. Here's the relevant part of my code:

import system
import time

class ToastManager:
    @staticmethod
    def open_toast(popup_id, view_path, params=None, duration=5):
        # Open the popup
        system.perspective.openPopup(popup_id, view_path, params)

        # Asynchronous task to close the popup after a delay
        def async_task():
            time.sleep(duration)  # Wait for the specified duration
            system.perspective.closePopup(popup_id)  # Attempt to close the popup

        system.util.invokeAsynchronous(async_task)
ToastManager.open_toast("myToastPopup", "PopupViews/MyToastView", {"message": "Hello, world!"}, 5)

The popup opens successfully, but the closePopup function does not work. If I call the async_task function directly it closes the popup with delay. But with invokeAsynchronous it does not close or throw any error.

Where I am getting wrong on this? Seems like the invoke invokeAsynchronous is not working. Where is this going wrong?

Platform
Ignition Platform 8.1.30 (b2023071408)

I only want to close the popup using project library scripting.

Questions

  1. Is it possible to call system.perspective.closePopup from an asynchronous thread using invokeAsynchronous?
  2. Do I need to use a different method to close the popup from a separate thread?
  3. Is there a recommended way to achieve this functionality in Ignition Perspective?

Sleep = bad.
Just add a timer on the view itself which closes itself. Set the time on load to a custom prop, and add a custom prop to get the difference in seconds between now and the first load prop and compare to your open seconds. If true, use an on change script to close the popup passing in "" as the view name

6 Likes