Author Topic: Interruptible waiting states  (Read 5433 times)

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Interruptible waiting states
« on: 2010 May 21 00:10:39 »
PixInsight's JavaScript engine is Mozilla's SpiderMonkey version 1.8 RC1 (the latest officially released version).

developer.mozilla.org/En/SpiderMonkey/Introduction_to_the_JavaScript_shell
Quote
sleep(dt)
Sleep for dt seconds. Fractions of a second are supported. Returns true on success, false if the sleep was interrupted.[/url]

Why PIJS command sleep(5) waits full 5 sec and always returns false ? I mean, why impossible interrupt sleep ?

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Interruptible waiting states
« Reply #1 on: 2010 May 21 00:53:56 »
Quote
Why PIJS command sleep(5) waits full 5 sec and always returns false  ? I mean, why impossible interrupt sleep ?

The sleep function is not part of the ECMA standard. It is a custom utility implemented in Mozilla's JavaScript shell js (which is actually a testing/demonstration tool for the SpiderMonkey engine).

PJSR's sleep function is a method of the Global object and has the following signature:

void sleep( Number seconds )

This method implements a non-interruptible wait state. Internally it has been implemented as a call to Sleep() on Windows and to usleep() on Linux/UNIX platforms.

To implement an interruptible wait, you must write a waiting loop yourself. For example:

Code: [Select]
#define WAIT_GRANULARITY 0.05 // in seconds

function MyDialog()
{
   // ...

   this.abortWait = false;

   this.wait = function( waitMaxSecs )
   {
      this.abortWait = false;
      var waitMaxSteps = Math.round( waitMaxSecs/WAIT_GRANULARITY );
      for ( var count = 0; count < waitMaxSteps && !this.abortWait; ++count )
      {
         sleep( WAIT_GRANULARITY ); // wait
         processEvents(); // process all pending GUI events
      }
      return !this.abortWait; // returns true if the wait was not interrupted
   };

   // ...

   this.abort_Button = new PushButton( this );
   this.abort_Button.text = "Abort";

   this.abort_Button.onClick = function()
   {
      this.abortWait = true;
   };

   // ...

   this.wait( 10 );
}

A future version of PixInsight (perhaps 1.6.1) will implement a new Timer object in PJSR. Timer will work either as a one-shot or as a continuous programmable timer with an onTimeout event handler that will be called at each timer interval.
« Last Edit: 2010 May 21 01:05:26 by Pleiades »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: Interruptible waiting states
« Reply #2 on: 2011 April 18 22:22:19 »
A future version of PixInsight (perhaps 1.6.1) will implement a new Timer object in PJSR.
It's implemented? Any instruction?