Looks like the ability to kill the running task is a needed feature, since someone already asked the same question and it has been answered here: Thread lifetime management.
Upcoming JXcore release will have jxcore.tasks.killThread()
. The logic is this: a task would inform the main thread, that it just has been started, and then the main thread may start counting the timeout for killing the thread, for example:
// main thread receives the message from a task
jxcore.tasks.on("message", function(threadId, obj){
if(obj.started){
//kill the task after a second
setTimeout(function(){
jxcore.tasks.killThread(threadId);
console.log("thread killed", threadId);
},1000);
}
});
// adding a task
jxcore.tasks.addTask( function() {
// informing the main thread, that task is just started
process.sendToMain({started:true});
// looping forever
while(true){};
console.log("this line will never happen.");
});