9 Replies Latest reply on Oct 29, 2014 10:26 AM by philmodjunk

    Enabling a cancel button in middle of a scripting loop?

    jfalberg

      Title

      Enabling a cancel button in middle of a scripting loop?

      Post

      Let me rephrase my question from Friday originally posted in http://forums.filemaker.com/posts/13c9a98787 but in a different inquiry.

      I have a process that goes into a loop and oftentimes takes up to 5 - 10 seconds to complete, but there may be times when it takes longer or perhaps gets stuck in an endless loop.  While management finds it unacceptable to press Esc and then to press another button to reset process variables, is there any way that while in a scripting loop besides pressing Esc to even press like a "Cancel" button?

        • 1. Re: Enabling a cancel button in middle of a scripting loop?
          philmodjunk

          Short of building some kind of limit testing in your script that avoids the infinite loop Esc is your only option in windows.

          Canceling any script in the middle of execution is a really, really bad idea. It's extremely difficult for any kind of follow on script to be able to "clean up" after the cancel.

          Can you describe what kind of infinite loop might occur? That sounds like a design error that should be fixed by modifying your design, not by adding a "cancel" capability.

          • 2. Re: Enabling a cancel button in middle of a scripting loop?
            jfalberg

            When the user presses a search button, it exports a record as an xml text file to another server which communicates with a web service then directly writes the results back.  An infinite loop may occur if a problem occurs when this other server fails to communicate with the web service.  I had also added a 30 second comparison to automatically stop the search but management still wants the ability to somehow press a button.

            Another question, is there any way to capture an instance of an Escape button so it can at least clear the processing variables without having to press another button to do so?

            • 3. Re: Enabling a cancel button in middle of a scripting loop?
              philmodjunk

              Can you show an example of that script to illustrate the loop involved?

              It might be possible to use get ( activeModifierkeys ) to detect the fact that a modifier key such as shift or Alt is down and then terminate the loop. This then would give you a controlled exit from the script via a specific keydown event.

              But I haven't tested this concept so you'd have to run a few tests with your actual script to see if it would work.

              • 4. Re: Enabling a cancel button in middle of a scripting loop?
                jfalberg

                Here's a snippet of the script we're using:

                Loop
                    Exit Loop if [Search::OutTag ≠ "X"]
                    If [Int(Get(CurrentHostTimeStamp) -- $$TimeStart) > 30]
                        Set Field [Search::OutTag; "C"]
                    End If
                    Refresh Window []
                End Loop

                Externally the 1 record OutTag value would be set to blank when the processing from another application gets completed and ready to view results.

                 

                • 5. Re: Enabling a cancel button in middle of a scripting loop?
                  philmodjunk

                  If you removed the IF block from this script, how/why would the value of OutTag ever change?

                  My earlier suggestion would look like this:

                  Loop
                      Exit Loop If [ Get ( ActiveModifierKey ) > 0 ]
                      Exit Loop if [Search::OutTag ≠ "X"]
                      If [Int(Get(CurrentHostTimeStamp) -- $$TimeStart) > 30]
                          Set Field [Search::OutTag; "C"]
                      End If
                      Refresh Window []
                  End Loop

                  I have not tested this, but see no reason why it would not work. You can look up this function in FileMaker help to learn more about what values this function can return.

                  • 6. Re: Enabling a cancel button in middle of a scripting loop?
                    jfalberg

                    I tested with the Get(ActiveModifierKeys) and it works when a Ctrl, Shift, Alt or Caps Lock is pressed so far, so I believe that should be acceptable, thanks Phil :)

                    We have an external processor with ODBC connection to the database that updates when ready to display results, so having a feature to reset processing variables certainly clean things up as desired.

                    • 7. Re: Enabling a cancel button in middle of a scripting loop?
                      philmodjunk

                      And if you look up that function you can alter this so that only a specific modifier key or combination of modifier keys will exit the loop. That change can help avoid accidentally terminating the script.

                      • 8. Re: Enabling a cancel button in middle of a scripting loop?
                        jfalberg

                        I'm using Ctrl+Shift (value = 5) since that seems less likely used in other applications.

                        • 9. Re: Enabling a cancel button in middle of a scripting loop?
                          philmodjunk

                          Good idea. Don't know if this is still the case, but we found that FileMaker could detect and respond to modifier keys even when running a completely different application and FileMaker was running in the background. That was several versions ago so this may have changed.