Deriving class from wxThread and wxEvtHandler to send messages to it

Vadim Zeitlin vadim at wxwidgets.org
Tue Jun 5 05:14:05 PDT 2007


On Tue, 5 Jun 2007 13:55:04 +0200 Volker Bartheld <dr_versaeg at freenet.de> wrote:

VB> I just stumbled over this section in the wxW-docu: "[...] However there is
VB> no built in method to send messages to the worker threads and you will
VB> need to use the available synchronization classes to implement the
VB> solution which suits your needs yourself. In particular, please note that
VB> it is not enough to derive your class from wxThread and wxEvtHandler to
VB> send messages to it: in fact, this does not work at all.
VB> [...]"
VB> (http://www.wxwidgets.org/manuals/stable/wx_wxthreadoverview.html#wxthreadoverview)
VB> 
VB> Hmmm. Strange that I didn't spot this earlier... And even stranger that the
VB> sample below which does exactly this (deriving a class WorkerThread :
VB> public wxEvtHandler, public wxThread) seems to happily update its counter
VB> m_uiCount through OnReset(), On Thread() and the wxEVT_THREAD event it
VB> receives - at least with the wxW versions (2.7x and 2.8x) and platforms I
VB> have tested.
VB> 
VB> So what's the background behind this statement "does not work at all" and
VB> how am I meant to do it right?

 At the very least by protecting m_uiCount from concurrent access. Your
OnThread() is currently called in the main thread context so it's not
surprising that it works -- the fact that it's in wxThread-derived class
doesn't mean anything.

 Regards,
VZ

-- 
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/





More information about the wx-users mailing list