Site Tools


Cancelling Scripts

Developer: RhinoScript
Summary: Discusses how to allow scripts to be cancelled by the user.

Question

I have encountered a problem when using RhinoScript. I am unable to interrupt a running script by pressing the ESC key. The problem show up when I write a tight loop or a recursive function. In order to cancel the script, I have to crash Rhino using Task Manager and restart everything. I do not have this problem with other scripts. Am I doing something wrong?

Answer

If you have a tight loop that does not call back into Rhino, then it is possible for ESC key processing to be either very slow or not happen at all. This is because the tight loop does not allow Rhino to process the messages, such as keystrokes, sent to it by Windows.

To work around this situation, you will want to call back into Rhino inside of your tight loop. Using RhinoScript's Sleep function is a good way to do this without slowing down your code.

For example:

 Sub TightLoopEscapeTest
   For i = 0 To 100000
     '
     ' Do tight loop processing here...
     ' 
     ' Allow Rhino to "pump" it's message queue
     Call Rhino.Sleep(0)
   Next
 End Sub

If your loop is relatively fast, you may want to postpone the Sleep call or else it will slow down your script significantly.

For example:

 Sub TightLoopEscapeTest
   For i = 0 To 100000
     '
     ' Do tight loop processing here...
     ' 
     ' Allow Rhino to "pump" it's message queue
     If ((i Mod 25) = 0) Then Call Rhino.Sleep(0)
   Next
 End Sub

Which will call the Sleep method only once every 25 iterations.

Using OnCancelScript to handle script cancelling

The default behavior when cancelling a script is to halt the script's execution and print a “Script cancelled” message to Rhino's command line. But, there are often times when you might want to know when your script is cancelled. For example, lets say you have a script that: (a) modifies some Rhino parameters, (b) performs an operation and, © resets the modified parameters. If your script is cancelled in operation (b), then Rhino can be left in a state unfamiliar to the user.

It is possible for your script to be notified when it has been cancelled. This is done through a special, user-defined subroutine named OnCancelScript.

When a script is running and the ESC key is pressed, RhinoScript searches for loaded, user-defined subroutine named OnCancelScript. If this subroutine is detected, RhinoScript will automatically run this procedure instead of just printing the “Script cancelled” message.

In the above script scenario, the user-defined OnCancelScript subroutine could reset the parameters © that were modified when the script started (a).

The following is a simple example that demonstrates the OnCancelScript procedure.

Sub TightLoopEscapeTest
 
  For i = 0 to 100000
 
    Call Rhino.Print(i)
 
    Call Rhino.Sleep(0)
 
  Next
 
End Sub
 
' User-defined cancel script handler

Sub OnCancelScript
 
  ' Do script cancelling operations here...

  Call MsgBox("Script Cancelled!", vbOkOnly + vbExclamation, "RhinoScript")
 
End Sub

Note, your user-defined OnCancelScript subroutine must not have any arguments. If you define your OnCancelScript subroutine as one that requires one or more arguments, then RhinoScript will not execute it when the ESC key is pressed.


developer/scriptsamples/cancelscript.txt · Last modified: 2014/06/26 (external edit)