clean shut down of wxpython

Hi!

I have some trouble to get a stable python program.
What I have is a multithreaded application where several threads can open
a window at different times.
Since I know that wxPython does not allow to open a Window from different
threads I first tried to
have one thread which gets signals when a new window should be opened.
Unfortunately that did not scale
very well (explaining the reasons would take too much time)

So my next idea was to have a single mutex on which the threads can wait.
And if a thread got the mutex it initializes wxPython, opens a window,
waits will the user pressed the ok
button and then shutds down wxPython.

Therefore no two threads would initialize wxPython at the same time.

But unfortunately this does not work well. wxPython still fails from time
to time and the dialog window
shows up but then hangs and neither controls are painted nor does the
window refresh.

Why is this so? And what else do I need to do to completely shutdown wxPython
Or is it impossible to to open wxPython in one thread, shutting it down
and reopening it in another thread?

Thanks a lot!, Fabian

import mutex
from wxPython.wx import *
import win32event

# Variable for wxPython bookkeeping
wxPythonApp = None
win = None

locked = False

try:

    # lock the mutex
    locked = wxPython_Mutex.testandset()
    while locked != True:
            tam_utilities.InvokeSleepEvent(0.1) # sleep 100 ms
            locked = _AD_.wxPython_Mutex.testandset()

    # do the wxPython stuff
    if locked == True:
        #print "we got the mutex, do wxPython stuff"
        wxPythonApp = wxPySimpleApp()

        win = wxTextEntryDialog(None, Message, Caption, DefaultValue, wxOK

wxCENTRE, wxDefaultPosition)

        win.CenterOnScreen()
        val = win.ShowModal()

        if val == wxID_OK:
            _AD_.Result = win.GetValue()
            _AD_.Status = 1
        else:
            _AD_.Result = ""
            _AD_.Status = 0

        del val

finally:
    # always clean up all wxPython stuff!!
    # otherwise a next wxPython execution might hang indefinitely in
MainLoop()
    try:
        if win != None:
            win.Destroy()
            del win
    except: pass

    try:
        if wxPythonApp != None:
            wxPytonApp.MainLoop()
            del wxPythonApp
    except: pass

    # unlock the mutex if we ever got it
    if locked == True:
        _AD_.wxPython_Mutex.unlock()

Hi!

Sorry this was all my fault, damn.
I once did what I knew is a bad idea, catching all exceptions with
a pass statement.

Reason for the catch is that I _must_ shutdown wxpython, otherwise
the application will hang.

But what I did not consider was that the code in the try block was wrong.
wxPytonApp instead of wxPythonApp :frowning:

Thanks, Fabian

ยทยทยท

    try:
        if wxPythonApp != None:
            wxPytonApp.MainLoop()
            del wxPythonApp
    except: pass