Requeuing an event - it seems to lock!

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
mattropolis
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Sep 17, 2007 11:30 pm

Requeuing an event - it seems to lock!

Post by mattropolis » Fri Oct 19, 2007 1:53 am

Hi,

I'm trying to do a rendering engine that needs to update as much as possible. So, I nievely did the following:
Made a new event type as per the example in the 'event' sample. I have a draw() function that that is triggered by that event. At the end of the draw() function, it simply posts an event that says to call draw() again.

unfortunately, this results in the system drawing as fast as it can (this is good), but you get no keyboard input and the app crashes in a few seconds.

Am I wrong in thinking that windows will continue to collect keyboard/mouse/timer events and post them to the event queue even if I'm handling an event? Did I write the event handler wrong?

Here's the relevant code - 99% lifted from the 'event' sample:

Code: Select all

BEGIN_DECLARE_EVENT_TYPES()
    DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777)
END_DECLARE_EVENT_TYPES()

DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)

// it may also be convenient to define an event table macro for this event type
#define EVT_MY_CUSTOM_COMMAND(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
        (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
        (wxObject *) NULL \
    ),

...

BEGIN_EVENT_TABLE(MyGLCanvas, wxGLCanvas)
	EVT_MOUSE_EVENTS(MyGLCanvas::OnMouseEvent)
	EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyGLCanvas::Draw)
END_EVENT_TABLE()

...
void MyGLCanvas::OnMouseEvent(wxMouseEvent& event)
{
if(event.LeftIsDown()) {
    wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
    wxPostEvent(this, eventCustom);}
}

....
void CGLCanvas::Draw( wxCommandEvent& event )
{
    //I do my drawing here
    wxCommandEvent eventCustom(wxEVT_MY_CUSTOM_COMMAND);
    GetEventHandler()->AddPendingEvent(eventCustom);
}

When the app starts, nothing happens till I hit the left mouse button when the first draw message is sent. It starts drawing, then never stops (what I want), but it also doesn't take any input and then hangs after a few seconds (bad).
Stuff I've tried:
1. If I remove the AddPendingEvent() at the end of draw, then it only draws once and stops (as expected).
2. Tried ProcessEvent() and wxPostEvent - same results.

So, why is this getting 'stuck' in an infinite loop? Shouldn't it just append the message to the end of the message queue? Shouldn't the OS still be able to queue up key/mouse events while I'm drawing?

Perhaps I should move the drawing routing into another thread that still gets triggered via events? It's a GL surface that doesn't take UI focus (since I know that only one thread can do ui at a time)

Post Reply