Save-Point
No Hang Script - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Material Development (https://www.save-point.org/forum-8.html)
+--- Forum: Scripts Database (https://www.save-point.org/forum-39.html)
+--- Thread: No Hang Script (/thread-2482.html)



No Hang Script - Zeriab - 03-06-2008

As many of you probably know you get a 'Script Hanging' error if a script takes too long to process.

More accurately, if the Graphics.update is not called in about 10 seconds the engine will assume an endless loop or something similar has happen. It will therefore give the 'Script Hanging' error.

This code snippet works by calling Graphics.update if this has not happened in a long time. A separate thread is running which checks up on the time. It should not affect the speed of the main thread in any significant way. It will not work if you have DLL calls which takes too long though.

THIS IS A SCRIPT YOU DON'T JUST ADD

Adding this script comes at the of your code becoming non-deterministic. This is bad, very bad. Also I do not have a monitor on critical parts, which gives the possibility of race conditions. I think that Graphics.update does have mutual exclusion, but I am not sure.

And even if it has, what do you think happens if Graphics.update is called while you are in the process of creating a sprite?
I expect this mainly to be for scripters to help their debugging and testing of stuff.

Since the script will not call Graphics.update unless 4 seconds has passed since the last call to Graphics.update I do not expect it will have too much of an effect.

Code:
if @zer_no_hang_stack.nil?
  ##
  # Change the Graphics module so it contains the time of the last update
  # Add a getter for the time property
  #
  module Graphics
    # Alias the update method (you have to do it this way since Graphics is a module)
    class << self
      alias no_hang_update update
    end
    ##
    # Change the update method
    #
    def self.update
      @@time = Time.now
      self.no_hang_update
    end
    ##
    # Retrieve the Time at the last update
    #
    def self.time
      # Protection if this method is called before the first update
      @@time = Time.now if @@time.nil?
      return @@time
    end
  end
  
  ##
  # A separate thread that will run and keep track of the time since the last
  # update
  #
  Thread.new {
    loop do
      # Lets the thread sleep for a while to minimize CPU usage
      sleep 1
      # If more than 4 seconds has passed since the last update
      if Time.now - Graphics.time > 4
        # Update the graphics
        Graphics.update
      end
    end
  }
  
  @zer_no_hang_stack = true
end

Use it wisely and only if there is an actual need.

*hugs*
- Zeriab