Prevent UI from freezing during long running Python script - Windows

I was about to post this earlier today but found the solution some time later, so consider this a little public service announcement:

Is there any way from preventing Rhino’s UI from locking up and the Rhino window from ‘Not Responding’ during the execution of a long python script?

Amazingly, I just searched the developer forum and couldn’t find anything on this subject, as it’s been a constant issue for me over the past few years.

I’m working on a deeply recursive, single threaded script that takes ages to complete and needed some indication of what was going on (via StatusBar messages), which of course don’t update when Rhino’s UI freezes.

What ended up working for me was to sprinkle:

Rhino.RhinoApp.Wait()  # Pauses to keep Windows message pump alive so views will update and windows will repaint.

At those points in the loops where I’d like Rhino’s main window to update.

1 Like

In addition I like to point to the rhinoscriptsyntax method Redraw()
As an example of forcing a single redraw of the viewports even when redrawing in disabled.

def Redraw():
    """Redraws all views
    Returns:
      None 
    Example:
      import rhinoscriptsyntax as rs
      rs.Redraw()
    See Also:
      EnableRedraw
    """
    old = scriptcontext.doc.Views.RedrawEnabled
    scriptcontext.doc.Views.RedrawEnabled = True
    scriptcontext.doc.Views.Redraw()
    Rhino.RhinoApp.Wait()
    scriptcontext.doc.Views.RedrawEnabled = old