More robust threading

Again, this is something that you can already do.

If you start a new thread with invokeAsynchronous, the thread will die when your function returns. This means that you’re probably doing something in a loop if your thread is staying alive. So to kill the thread - flip some flag that makes the loop stop. i.e.:

global canceled
while not canceled:
  ... do something ...