1.6.1Script lifecycle

Scripts run forever until they call Framework.EndScript(), or the user shuts them down. They don't simply run "from top to bottom" of their code and then automatically terminate. This is because most operations in the framework are asynchronous, and the script may be waiting for an action to complete. The script needs to tell the framework when it has finished.

Consider the following example:

Framework.Ask("Are you sure?", function (Msg) {

// Called asynchronously by the framework

});

Framework.EndScript();

This won't work. The script will create the ask dialog, but then immediately terminate (also destroying the dialog) before it has got a response. This script should instead be written as follows:

Framework.Ask("Are you sure?", function (Msg) {

// Called asynchronously by the framework

// … do something based on the user's response

// And, finally, tell the framework that we're done

Framework.EndScript();

});

// When execution gets here, we're waiting for the user's response to the Ask().

// We don't want to terminate.