Thread Functions
Overview
- Thread module functions manage thread-related operations
- The thread module uses the
threadprefix, e.g.thread.execFuncSync()
setTimeout Delayed Execution
- Run a function after a delay, in a child thread
- @param func Function to execute
- @param timeout Delay in milliseconds
- Returns a thread object that can be cancelled
function main() { var t = setTimeout(function () { toast("This runs after one second"); }, 1000); // Simulate script running while (true) { sleep(1000) }}
main();cancelTimeout Cancel Delayed Execution
- Cancel a delayed execution
- @param t Function/thread to cancel
function main() { var t = setTimeout(function () { toast("This runs after one second"); }, 1000); // After cancel, it will not run cancelTimeout(t);}
main();setInterval Periodic Execution
- Run a function on an interval, in a child thread
- @param func Function
- @param interval Interval in milliseconds
- @return Thread object that can be cancelled
function main() { var t = setInterval(function () { toast("This runs every second"); }, 1000); // Simulate script running while (true) { sleep(1000) }}
main();cancelInterval Cancel Periodic Execution
- Cancel a periodic execution
- @param t Function/thread to cancel
function main() { var t = setInterval(function () { toast("This runs every second"); }, 1000); cancelInterval(t);}
main();execSync Synchronous Execution
- Run a function and wait until it returns true; returns immediately when the callback returns true
- @param condition Condition function
- @param timeout Timeout in milliseconds
- @return boolean
function main() { execSync(function () { logd("This runs synchronously"); }, 1000);}
main();thread.stopAll Stop All
- Cancel all running threads
function main() { execSync(function () { logd("This runs synchronously"); }, 1000); thread.stopAll();}
main();thread.execAsync Async Execution
- Run asynchronously; the Runnable is managed by a thread pool
- @param runnable Runnable object
function main() { var tid = thread.execAsync(function () { while (true) { logd("This runs asynchronously"); sleep(1000); if (thread.isCancelled(tid)) { break; } } }); logd("tid " + tid); // Cancel thread after 5s sleep(5000); logd("Cancel thread " + tid); thread.cancelThread(tid); sleep(5000); logd("Done ");}
main();thread.execSync Synchronous Execution
- Run a function and wait until it returns true; returns immediately when the callback returns true
- @param condition Condition function
- @param timeout Timeout in milliseconds
- @return boolean
function main() { thread.execSync(function () { logd("This runs synchronously"); }, 1000);}
main();thread.cancelThread Cancel Thread
- Cancel thread execution
- @param t Thread ID
- @return
{boolean}
function main() { var tid = thread.execAsync(function () { while (true) { logd("This runs asynchronously"); sleep(1000); if (thread.isCancelled(tid)) { break; } } }); logd("tid " + tid); // Cancel thread after 5s sleep(5000); logd("Cancel thread " + tid); thread.cancelThread(tid); sleep(5000); logd("Done ");}
main();thread.isCancelled Cancellation Check
- Check whether a thread was cancelled
- @param t Thread ID
- @return boolean true if cancelled, false if not
function main() { var tid = thread.execAsync(function () { while (true) { logd("This runs asynchronously"); sleep(1000); if (thread.isCancelled(tid)) { break; } } }); logd("tid " + tid); // Cancel thread after 5s sleep(5000); logd("Cancel thread " + tid); thread.cancelThread(tid); sleep(5000); logd("Done ");}
main();