Hi folks,
I'm working on a wxPython project that runs background threads behind
ProgressDialogs.
Previously, only one such dialog existed in the project. The code to
handle the whole process was very ad-hoc and ugly, but it worked.
I am now adding several more tasks that display progress dialogs, so I
decided to DRY/clean up my approach. I thought a generator that
yielded the next message to display on the ProgressDialog could do the
thread's real work, and a subclass of threading.Thread could handle
the bookkeeping and communication with the dialog.
Having implemented the approach, I have found that it almost works. A
dialog is displayed, progress is made, and the 'success' code written
by the user is run once the task is finished.
However, my code appears to freeze the main thread somehow - if you
try to click the Cancel button during processing, no events seem to be
triggered, and eventually the 'processing' cursor will be displayed.
I thought I had used wx.CallAfter() everywhere I update GUI components
from the background thread, and that this would be sufficient to keep
the UI responsive.
I've spent some time poring over my code and trawling the web, trying
to piece together why my old code worked and my new approach does not.
I have not been able to work it out thus far, and now seek advice from
those wiser than me.
Obviously, I've misunderstood something. What, I'm not sure.
I should note that I found running even a short time.sleep() inside
the ProgressThread.do_work() generator makes this problem 'go away'. I
doubt that's relevant, but I thought it was worth mentioning.
Here's a gist with the sample file. This was tested on Mac OS X
10.6.8, with wxPython 2.8.10.1 (Unicode version), and Python 2.6.5.
https://gist.github.com/2577876
You should be able to download it, unpack it, make progress.py
executable, and do './progress.py' on a *nix system.
(The old code is very entangled with app-specific functionality and
does not extract to an example at all, so I have not included it.)
Can anyone explain what I'm doing wrong?
Thanks for reading.
-Nate