ApplescriptObjc Exit Thread (NSThread)

I am working on an app that invoves using NSThread to distrubite the workload to a second thread, but I cannot close the thread. It would like to have the app cleaning up it's resources whenever possible, but whatever I try does not work. To start a thread, I use:

property downloadThread : class "downloadThread"      

set threadTwo to (NSThread's detachNewThreadSelector_toTarget_withObject_("test:", downloadThread,"Download File"))

to start a thread, but it won't close. I've tried using these in the second thread's script:

property NSThread : class "NSThread"


NSThread's close_()
NSThread's exit_()
NSThread's exit_(NSThread's currentThread)

They all return an error saying that an unrecognized selector was sent to the class. I would appreciate any help on this..

In general there’s no way to force a thread to terminate. The issue is that the thread might be holding on to shared resources and, if you just kill the thread, those resources become unavailable to the rest of your app.

The only safe way to stop a thread is to use some sort of inter-thread messaging to tell it to stop. How you do that depends on the nature of the work your doing with the thread. If the work can be broken up into discrete units, I generally recommend that you implement it as an NSOperation subclass. NSOperation has specific support for cancellation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
ApplescriptObjc Exit Thread (NSThread)
 
 
Q