Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:cancelscript [2015/09/14] (current)
Line 1: Line 1:
 +====== Cancelling Scripts ======
 +> **Developer:​** //​[[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //Discusses how to allow scripts to be cancelled by the user.//
 +
 +=====Question=====
 +I have encountered a problem when using [[developer:​rhinoscript|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:
 +
 +<code vb>
 + 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
 +</​code>​
 +
 +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:
 +
 +<code vb>
 + 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
 +</​code>​
 +
 +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, (c) 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 (c) that were modified when the script started (a).
 +
 +The following is a simple example that demonstrates the OnCancelScript procedure.
 +
 +<code vb>
 +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
 +</​code>​
 +
 +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.
 +
 +
 +\\
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/cancelscript.txt ยท Last modified: 2015/09/14 (external edit)