wxEventPost problem Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
valiyuneski
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Dec 11, 2008 3:01 pm

wxEventPost problem

Post by valiyuneski » Thu Dec 11, 2008 3:09 pm

Hi i have a wxWindow which creates a thread, from within the thread i need to post a message to owner of this thread which is a GUI but for some reason the wxWindow message PUMP does not call the mapped function:

1. wxWindow.cpp defines the new custom event with which is going to be called

Code: Select all

BEGIN_DECLARE_EVENT_TYPES()
   DECLARE_LOCAL_EVENT_TYPE(wxEVT_THREAD, wxNewEventType())
END_DECLARE_EVENT_TYPES()
DEFINE_LOCAL_EVENT_TYPE(wxEVT_THREAD)

void wxVUMeterWnd::OnThreadEvent(wxEvent& event)
{
   // Getting Event Data
   wxString strLog = ((wxCommandEvent*)&event)->GetString();
}

void CreateThread()
{
   m_pCondition = new wxCondition(m_Mutex);
   m_pThread = new wxVUThread(GetEventHandler(), &m_Mutex, m_pCondition);
}
2. inside Thread

Code: Select all

   // Post Event To Prant Window
   wxCommandEvent event(wxEVT_UPDATE);
   event.SetString(wxT("Thread::Run"));
   wxPostEvent(m_pEvtHnd, event);
   m_pEvtHnd->AddPendingEvent(event);
And i do not receive the message ..

Any Ideas would be highly appreciated

EDIT by Auria : please use code tags

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Dec 11, 2008 6:40 pm

Could you show how you bind OnThreadEvent to receive events? (disclaimer: i never used local events so i may miss something)

valiyuneski
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Dec 11, 2008 3:01 pm

Post by valiyuneski » Thu Dec 11, 2008 8:08 pm

This is the Window Message Mapping

1. Message Map + Custom Event
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_LOCAL_EVENT_TYPE(wxEVT_THREAD, wxNewEventType())
END_DECLARE_EVENT_TYPES()
DEFINE_LOCAL_EVENT_TYPE(wxEVT_THREAD)

2.Binding the custom message to be caught when a message is posted
void wxVUMeterWnd::OnThreadEvent(wxEvent& event)
{
// Getting Event Data
wxString strLog = ((wxCommandEvent*)&event)->GetString();
}

3. Creation of the Thread
void CreateThread()
{
m_pCondition = new wxCondition(m_Mutex);
m_pThread = new wxVUThread(GetEventHandler(), &m_Mutex, m_pCondition);
}

4. Within the thread i post the custom message as follows :

wxCommandEvent event(wxEVT_THREAD);
event.SetString(wxT("Thread::Run"));
wxPostEvent(m_pEvtHnd, event);

i also tried
m_pEvtHnd->AddPendingEvent(event);

With no success receiving it ...

lvdlinden
Earned some good credits
Earned some good credits
Posts: 147
Joined: Thu May 17, 2007 7:03 am
Location: 's-Hertogenbosch, Netherlands

Post by lvdlinden » Mon Dec 15, 2008 11:02 pm

As Auria stated, you haven't shown how you connect the event type wxEVT_THREAD to the handler function (wxVUMeterWnd::OnThreadEvent). A handler function needs to be registered for the event handler to handle the event, otherwise the event is simply ignored or propagated to it's parent window (in case of wxCommandEvent).

To connect wxCommandEvent events of type wxEVT_THREAD to wxVUMeterWnd::OnThreadEvent, add this line to the constructor of wxVUMeterWnd:

Code: Select all

Connect( wxEVT_THREAD, wxCommandEventHandler(wxVUMeterWnd::OnThreadEvent) );
You can also use the BEGIN_EVENT_TABLE/END_EVENT_TABLE macros, if you prefer those instead.

By using wxCommandEventHandler, the method OnThreadEvent can take a wxCommandEvent& as parameter, so there is no need to downcast:

Code: Select all

void wxVUMeterWnd::OnThreadEvent(wxCommandEvent& event)
{
   // Getting Event Data
   wxString strLog = event.GetString();
}
Regards,

Lennaert

Post Reply