From: Kevin A. <al...@se...> - 2004-07-10 02:48:51
|
Alex provided some good info. The findfiles tool, hopalong, life, and spirograph samples all make use of SafeYield so I suggest taking a look at those for various ways of starting a long running process that doesn't involve using threads. I particularly like the use of a generator in the hopalong sample and just like the other samples mentioned above it handles starting a routine and allowing the user to stop the execution in a clean way. Other samples such as webgrabber, chat, jabberChat, webserver show how to use threads, but using threads is more complicated and error-prone and you probably don't need them. ka On Jul 9, 2004, at 8:39 AM, Alex Tweedly wrote: > At 14:01 09/07/2004 +0200, pie...@ba... wrote: > >> Hello, >> >> I have the following problem: >> >> I'm writing a PythonCard application that has 2 buttons (a start >> button and >> a stop button). >> >> When I press the start button, the 'on_cmdStart_mouseClick(self, >> event):' >> function is executed. >> Now, I want to be able to stop that function at any time by pressing >> the >> STOP button. >> >> My problem is now that 'mouseClick'-events are not seen as long as >> I'm in >> the 'on_cmdStart_...' function. >> >> Can anyone help me? > > It's not just mouse-Click events that aren't seen - while your code is > running, no events will be processed. > > You should read (or re-read to remind yourself) about > http://pythoncard.sourceforge.net/timers-threads.html > > Note in particular the introductory paragraph that says: > >> Most of the top-level code you add to the front ends - your >> PythonCard user interfaces - is event handlers. As with programming >> with any GUI, event handlers should always complete their job fast, >> and return promptly, so they don't 'clog up the works'. PythonCard is >> no exception. > > While an event handler is running, no further events can happen. The > cleanest way to deal with processing that takes a long time is using > threads - and is well explained in that tutorial. > > If you have a really simple case, you can use the wxApp function Yield > - but read the cautions about its dangers - see the wx documentation > for wxApp::Yield > > Here's a simple bit of code that shows what this does > > Notes. > 1. the work this does to keep it busy is calculate factorials - hey, > I've always wanted a chance to write a recursive factorial function > for a good purpose :-) > 2. each time round the loop, it calls yield to allow other events to > be processed - without this the Stop button click wouldn't happen, and > in fact the writing and updating of the TextArea would happen. (try > commenting out the Yield line to see it fail ....) > 3. the "app" in "app.Yield()" is the global from the main program (not > included here, but the standard name used in the templates). > > > def on_openBackground(self, event): > self.results = [] > > def on_Start_mouseClick(self, event): > def fact(i): > if i<=1: return 1 > return i * fact(i-1) > > self.stopping = False > for i in range(1000): > j = fact(i) > s = "%d! is %d" % (i, j) > ## self.results.append(s) > ## self.components.TextArea1.text = "\n".join(self.results) > self.components.TextArea1.text = str(i) > app.Yield() > if self.stopping: break > > def on_Stop_mouseClick(self, event): > self.stopping = True > > > -- Alex. > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.714 / Virus Database: 470 - Release Date: 02/07/2004 |