developer:scriptsamples:cancelscript


Table of Contents

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.


developer/scriptsamples/cancelscript.txt · Last modified: 2010/04/19 09:16 by dale Driven by DokuWiki Recent changes RSS feed

 © 1997-2012 

McNeel North America Europe Latin AmericaAsia