To prevent long running scripts to freeze the application it may be necessary to add a way to timeout the execution. This article presents two different ways to do so.
The line callback feature can be used to perform some special treatment during execution of the scripts. The callback is called for every script statement, which makes it possible to verify if the script has
executed for too long time and if so suspend the execution to be resumed at a later time.
Before calling the context's Execute method, set the callback function like so:
Take a look at the sample Events to see this working.
Observe that if the script is compiled with asEP_BUILD_WITHOUT_LINE_CUES, the line callback will be invoked less frequently, though it is guaranteed to be invoked at least for every loop or function call.
A second thread can be set up to suspend the execution after the timeout. This thread can then be put to sleep so that it doesn't impact performance during the execution. When the thread wakes up it should call the context's Suspend method.
The below shows some possible code for doing this. Note that the code for setting up the thread is fictive, as this is different for each target OS.
Observe that this way of doing it is safe even if the AngelScript library has been built without multithread support.