Threads - Best Practice to end early

I have not used threads until recently. I converted a “Fire Once” timer to a thread and it was pretty much a no-brainer switch. I had used threads in another language and it was a bit tedious to manage so I had not explored them in XOJO and generally not needed them until recently.

What is the best way to end a thread prematurely. It appears that if it executes to the “bottom” of the code it ends and won’t start again until you do a thread.RUN.

If I need to end a thread (maybe due to an error) should it KILL itself or do something like EXIT SUB?

Thanks.

In the LR about KILL http://documentation.xojo.com/index.php/Thread.Kill

It is said it is a graceful exit …

Thanks. I suppose if I was to the read the documentation it would help.

Well at least I only re-purposed a few electrons (since you can’t kill them) and no trees. ;-))

If you need to end gracefully, add a Boolean property to your thread class named StopEarly. Then in the threads run loop, check the Boolean. If it’s set to true, run your cleanup routine and then return. From the outside, all you need to do is set the StopEarly property to true.

Greg,

Yep my question was from within the thread code itself detecting it needed to end but I see how adding the Boolean “flag” could be set to force any “housekeeping” before the exit.

When you say “return” is it acceptable to do an EXIT SUB?

Thanks.

You could, but return with no value will work too.

Check this: https://drive.google.com/file/d/0B6w6JZen_vW2eDRrb3dSZURHcnc/edit?usp=sharing