« Image view for FileMa… | Home | Playing with CoreML i… »

Problems with killing Xojo threads with plugin calls.

While you can kill a thread in Xojo, you may think twice before doing it.
The kill command usually raises a ThreadEndException on the thread which with normal exception handling exists all methods and returns to the run event, which on exit ends the threads. So with normal Xojo code this should often work fine.

Now things are different when one of my MT methods in the plugin run. Kill command causes problems there including crashes and runtime assertions. In my MT methods the plugin will run a loop and yield time to other Xojo threads. The actual work happens on a preemptive thread in parallel. The plugin doing the loop has currently no way to know you called kill, so it will continue until the work is done on the preemptive thread and return with the result. But yielding time may cause the ThreadEndException to be processed. The thread can be released from memory, objects destroyed and that pulls the rug out from under the plugins feet. The plugin continues trying to get the work done and this causes major problems for some client.

The better way usually is to define a cancel flag on the thread class and set it to true, when thread should be considered killed. In the thread, you could regularly check the flag and exit code immediately when it's true. This way you can exit safely and do clean up which may not happen when using kill, e.g. delete temp files.

For the future, the new thread class has no kill method.

Here a list of MBS Plugin functions with preemptive threading:
15 09 17 - 12:19