def delegate(cls): if not NSThread.isMainThread(): thread = threading.current_thread() pool = getattr(thread, 'ns_autorelease_pool', None) if pool is None: print("--- calling NSApp.delegate() without an autorelease pool from {}".format(thread)) traceback.print_stack() return NSApp.delegate()
def call_in_gui_thread(func, *args, **kwargs): if NSThread.isMainThread(): func(*args, **kwargs) else: pool = NSAutoreleasePool.alloc().init() NSApp.delegate().performSelectorOnMainThread_withObject_waitUntilDone_( "callObject:", lambda: func(*args, **kwargs), False) del pool
def performCall(self): """ Actually runs the payload. """ assert NSThread.isMainThread(), "Call is not executing on the Main thread!" # Unpack the payload. (func, args, kwargs) = self._payload # Run it. func(*args, **kwargs)
def scheduleCallWithDelay_(self, delay): """ This is run once we're on the Main thread. """ assert NSThread.isMainThread(), "Call is not executing on the Main thread!" # There's no delay, just run the call now. if not delay: self.performCall() return # There's a delay, schedule it for later. self.performSelector_withObject_afterDelay_(self.performCall, None, delay)
def scheduleCallWithDelay_(self, delay): """ This is run once we're on the Main thread. """ assert NSThread.isMainThread(), "Call is not executing on the Main thread!" # There's no delay, just run the call now. if not delay: self.performCall() return # There's a delay, schedule it for later. self.performSelector_withObject_afterDelay_( self.performCall, None, delay, )
def wrapper(*args, **kw): if NSThread.isMainThread(): func(*args, **kw) else: NSApp.delegate().performSelectorOnMainThread_withObject_waitUntilDone_("callObject:", lambda: func(*args, **kw), False)
def call_in_gui_thread(func, *args, **kwargs): if NSThread.isMainThread(): func(*args, **kwargs) else: NSApp.delegate().performSelectorOnMainThread_withObject_waitUntilDone_("callObject:", lambda: func(*args, **kwargs), False)