From: James Bunton Date: Sun, 2 Jan 2011 14:47:29 +0000 (+1100) Subject: Make cancel button actually stop the background thread X-Git-Tag: notipod-1.2 X-Git-Url: https://code.delx.au/notipod/commitdiff_plain/refs/tags/notipod-1.2?ds=sidebyside Make cancel button actually stop the background thread --- diff --git a/notipod_gui.py b/notipod_gui.py index ee97365..084b1d7 100644 --- a/notipod_gui.py +++ b/notipod_gui.py @@ -111,7 +111,7 @@ class NotiPodController(NSObject): loadingLabel = objc.IBOutlet() def awakeFromNib(self): - self.gen = None + self.runningGenerator = False # Delegate methods def applicationWillFinishLaunching_(self, _): @@ -132,6 +132,8 @@ class NotiPodController(NSObject): # Utility methods def runGenerator(self, func, finish): + assert not self.runningGenerator + self.runningGenerator = True NSApp.beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(self.loadingSheet, self.window, None, None, None) arg = (func(), finish) self.performSelectorInBackground_withObject_(self.runGeneratorThread, arg) @@ -139,21 +141,24 @@ class NotiPodController(NSObject): def runGeneratorThread(self, (gen, finish)): pool = NSAutoreleasePool.alloc().init() for msg in gen: + if not self.runningGenerator: + break self.loadingLabel.performSelectorOnMainThread_withObject_waitUntilDone_( self.loadingLabel.setStringValue_, msg, True) self.performSelectorOnMainThread_withObject_waitUntilDone_( self.stopGenerator, finish, True) + self.runningGenerator = False del pool def stopGenerator(self, finish): + self.runningGenerator = False NSApp.endSheet_(self.loadingSheet) self.loadingSheet.orderOut_(self) - if finish: - finish() + finish() @objc.IBAction def doCancel_(self, sender): - self.stopGenerator(None) + self.runningGenerator = False @objc.IBAction def doSync_(self, sender):