since my problems with threads and events (http://forums.wxwidgets.org/viewtopic.php?t=24663) I am fighting with custom events.
I combined the example from the above topic with this one:
http://wiki.wxwidgets.org/Custom_Events ... -_Method_4
I want to send a struct from one thread to another one, but when processing the sent data, it is invalid.
Sending the event itself works. Sending a only a string also works.
Whats wrong?
Thanks,
mspoerr
Custom Event Code:
Code: Select all
#ifndef __LOGEVENT__
#define __LOGEVENT__
#include <boost/any.hpp>
#include <wx/event.h>
DECLARE_EVENT_TYPE( EVT_LOG_MELDUNG, -1 )
class LogEvent: public wxEvent
{
boost::any data;
public:
LogEvent () : wxEvent(0, EVT_LOG_MELDUNG) { }
LogEvent (const boost::any& data) : wxEvent(0, EVT_LOG_MELDUNG), data(data) { }
~LogEvent () { }
wxEvent* Clone () const { return new LogEvent(*this); }
void SetData (const boost::any& data) { this->data = data; }
boost::any GetData () const { return data; }
template<typename T> T GetData () const { return boost::any_cast<T>(data); }
};
typedef void (wxEvtHandler::*LogEventFunction)(LogEvent &);
#define LogEventHandler(func) \
(wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)\
wxStaticCastEvent(LogEventFunction, &func)
// Define the event table entry. Yes, it really *does* end in a comma.
#define EVT_LOG(id, fn) \
DECLARE_EVENT_TABLE_ENTRY( EVT_LOG_MELDUNG, id, wxID_ANY, \
(wxObjectEventFunction)(wxEventFunction) \
(wxCommandEventFunction) wxStaticCastEvent( \
LogEventFunction, &fn ), (wxObject*) NULL ),
#endif
Code: Select all
void Ersetzer::schreibeLog(int type, string logEintrag, int farbe, int format, int groesse)
{
if (logAusgabe != NULL)
{
WkLog::evtData evtDat;
evtDat.farbe = farbe;
evtDat.format = format;
evtDat.groesse = groesse;
evtDat.logEintrag = logEintrag;
evtDat.type = type;
LogEvent evt(EVT_LOG_MELDUNG);
evt.SetData(&evtDat);
wxPostEvent(logAusgabe, evt);
}
}
Code: Select all
void WkLog::OnSchreibeLog1(LogEvent &event)
{
boost::any data = event.GetData();
if (data.type() == typeid(evtData*))
{
evtData *evtDat = event.GetData<evtData*>();
schreibeLog(evtDat->type, evtDat->logEintrag, evtDat->farbe, evtDat->format, evtDat->groesse);
}
}