The kill() method is just a stub, written back when I thought the way to do this is for the main thread to terminate the worker thread. Now, after more reading, I realize it's better to have the worker test a condition periodically and exit (gracefully) if necessary.

The withObject_ parameter can be used to pass a reference back to the original thread. This is useful when the new thread executes a method in a different class. I ended up just calling a method in the same class, like this:

from Foundation import *
from AppKit import *
import objc, time

from PyObjCTools import NibClassBuilder

class PyThreadAppDelegate(NibClassBuilder.AutoBaseClass):
    def init(self):
        self = super(PyThreadAppDelegate, self).init()
        return self

    def go_(self, sender):
        print 'go_'
        self.continueFlag = True
        #t = Threaded.alloc().init()
        NSThread.detachNewThreadSelector_toTarget_withObject_(
            'newThread:', self, None)

    def kill_(self, sender):
        print 'kill_'
        self.continueFlag = False

    @objc.signature('v:@')
    def newThread_(sender):
        pool = NSAutoreleasePool.alloc().init()
        print 'Threaded', sender
        for i in range(10):
            if sender.continueFlag:
                time.sleep(1)
                print i
        del pool

Thanks.
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to