Wait for confirmation in for loop

Hi All,

Does anyone know how to handle confirmation in a for loop in Perspective?

In Vision, I believe the system.gui.messageBox in a for loop would pause the for loop execution until the user presses the confirm button.

Is there a way in Perspective to handle this? Does using system.util. invokeAsynchronous and time.sleep a good enough workaround? so that users have enough time to reaction before the for loop continues.

I appreciate any input! Happy Holiday!

From what I have learned, using sleep in UI is bad practice. In vision, calling the sleep function from the UI thread will cause anything else in the UI thread to freeze. I'm not sure of the behavior in perspective, but I assume its the same. I believe the same idea would be applied for messageBox too.

Calling sleep in Perspective won't lock the browser, but it does your tie up a thread, so the general recommendation to avoid it still applies.

1 Like

For this I would use a message handler.
On the button that launches the confirmation, add a message handler that does your actions after confirmation. The button opens the confirmation popup and the accept button calls the message handler

2 Likes

If it’s just one confirmation, the message handler will work fine. But this is in a for loop, which means it could have multiple confirmations. So how can I pause the for loop execution for each confirmation?

That would be a terrible solution. If the user gets distracted the loop will continue.

  • Create a custom variable on the view. Call it acknPointer : 0 or similar.
  • Put your confirmation loop script in an onChange event on acknPointer. Modify it to do the popup on change to a non-zero value and just return without doing anything if it == 0.
  • Modify the popup to accept the acknPointer variable as a parameter. On pressing the OK button increment the acknPointer and pass it back in the sendMessage payload.
  • Use the message listener to copy the payload's acknPointer to the view's custom property which will increment it by one and retrigger the onChange script.
  • On the last acknowledge set ackPointer to zero which will cause the onChange to abort early.
1 Like

Thank you! I think the while loop will be able to make it work with the method you mentioned here.

No it won't. Don't do that because something will have to hang while waiting for a response. This would be a really bad design decision.

My solution runs a sequence of events which respond to triggering actions - either onChange or sendMessage. There is no loop. There is no wait or sleep. There is no while. Each time a change occurs the code executes and exits. Read it again!

Okay I see what you mean. I’ll give it a try. Basically I will be tracking the for loop index with the custom property and execute the script that way

Yes. I should have said that your script will go into acknPointer's property change script. (Right-click on the custom property.)

Let's say you have five checks to do then set acknPointer = 1.

  • That will trigger acknPointer's Property Change script which will open the popup (and exit the Property Change script).
  • When the user responds to the popup use sendMessage to send '2' to the message handler on acknPointer. The listener will update the pointer and do whatever has to be done with that row of the dataset. Also close the popup (by a line in the script - not by a navigation event as these running both a script and navigation in the same event can cause problems do to executing in the wrong order).
  • The change in acknPointer will trigger the Property Change event on the custom prop.

Each bit of code does the job and exits.