Problems with wxThread::Wait in a dll under windows

Marco Gubernati marco.gubernati at erxa.it
Tue Dec 5 02:29:37 PST 2006


Hi,

I've got a dll that uses some features of wxWidgets. For example
threads. Inside the dll no need to use any GUI functions, so I simply
use wxInitialize/Uninitialize, in order to be able of starting threads.

Under Linux everything works, both if I link the dll(.so) against a
wxWidgets GUI application and a generic application.

Under Windows, if I link the dll against a GUI application not based on
wxWidgets (for example a MFC one) threads
 remain on an infinite loop inside the wxThread::Wait()..
I've inspected the code (wx 2.6.3 and 2.7.2, the following parts are
exactly the same in both version).

first, the wxInitialize creates an application object of class
wxConsoleApp

the wxThread::Wait() executes the following peace of code

    do
    {    [...]

        result = ::MsgWaitForMultipleObjects
                 (
                   1,              // number of objects to wait for
                   &m_hThread,     // the objects
                   false,          // don't wait for all objects
                   INFINITE,       // no timeout
                   QS_ALLINPUT|QS_ALLPOSTMESSAGE   // return as soon as
there are any events
                 );

        switch ( result )
        {
            case 0xFFFFFFFF:
                // error [...]

            case WAIT_OBJECT_0:
                // thread we're waiting for terminated
                break;

            case WAIT_OBJECT_0 + 1:
                // [...]
                if ( wxThread::IsMain() )
                {
                    // it looks that sometimes WAIT_OBJECT_0 + 1 is
                    // returned but there are no messages in the thread
                    // queue -- prevent DoMessageFromThreadWait() from
                    // blocking inside ::GetMessage() forever in this
case
                    ::PostMessage(NULL, WM_NULL, 0, 0);

                    wxAppTraits *traits = wxTheApp ?
wxTheApp->GetTraits()
                                                   : NULL;

                    if ( traits && !traits->DoMessageFromThreadWait() )
                    {
                        // WM_QUIT received: kill the thread
                        Kill();

                        return wxTHREAD_KILLED;
                    }
                }
                break;

            default:
                wxFAIL_MSG(wxT("unexpected result of
MsgWaitForMultipleObject"));
        }
    } while ( result != WAIT_OBJECT_0 );

The problem is that, in the case of a wxConsoleApp, the
DoMessageFromThreadWait() DOES NOTHING, so the thread
continues to return from the MsgWaitForMultipleObjects with code
WAIT_OBJECT_0 + 1 (because the application that links with my dll is a
GUI one that receives messages, but DoMessageFromThreadWait does not
process them)

Any suggestions?? Maybe there's a better way to Initialize/Uninitialize
wxWidgets inside a dll that solves it (remember that under Linux
everything works)... but, apart from this, I don't think it's very
sensible to wait for messages that will not be processed, so, for
example I would modify the wx code as follow

test if it is a wxConsoleApp;

if it is

        result = ::MsgWaitForMultipleObjects
                 (
                   1,              // number of objects to wait for
                   &m_hThread,     // the objects
                   false,          // don't wait for all objects
                   INFINITE,       // no timeout
                   0;
                 );
else

        result = ::MsgWaitForMultipleObjects
                 (
                   1,              // number of objects to wait for
                   &m_hThread,     // the objects
                   false,          // don't wait for all objects
                   INFINITE,       // no timeout
                   QS_ALLINPUT|QS_ALLPOSTMESSAGE   // return as soon as
there are any events
                 );

Comments???? Thanks.

MARCO GUBERNATI

marco.gubernati at erxa.it

http://www.erxa.it
------------------------------------------------------------------------
-------
|E______| ERXA srl                         ISO 9001 certified
|__R____| C.so Svizzera, 185 - 10149 TORINO - ITALY
|____X__| Tel.: +39 - 011 741 27 49
|______A| Fax: +39 - 011 741 20 44

========================================================================
==

Questa mail e gli eventuali attachments possono contenere informazioni
confidenziali e riservate e non soggette a divulgazione ai sensi di
legge e sono da considerarsi dirette esclusivamente ai destinatari
indicati. E' fatto divieto a chiunque altro di leggere, copiare,
diffondere o utilizzare in qualsivoglia maniera tali informazioni. Se
avete ricevuto questo messaggio per errore, siete pregati di cancellarlo
e di avvisarne il mittente.

========================================================================
===

This message and its attachments (if any) may contain confidential,
proprietary or legally privileged information and is intended only for
the use of the addressee named above. No confidentially or privilege is
waived or lost by any mistranmission.

If you are not the intended recipient of this message you are hereby
notified that you must noy use, disseminate, copy it in any form or take
any action in reliance on it. If you have received this message in error
please delete it and any copies of it and kindly inform the sender of
this e-mail by replying or go to www.erxa.it "contact us".





More information about the wx-users mailing list