Simple little event handler.

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
*DEAD*
Experienced Solver
Experienced Solver
Posts: 60
Joined: Thu Jun 21, 2007 1:53 am

Simple little event handler.

Post by *DEAD* » Fri Jan 18, 2008 10:12 am

Hi, I hope this is useful to someone, i am in the process of building a application which can support multiple clients, and each client needed its own event handler so it did not block another client. From what i can tell, wx has no eventhandler which can run in a separate thread from the main thread, so i made my own. Ive simplified it a little bit, but hopefully its still useful.

EventHandler.h

Code: Select all

class EventHandler : public wxThread
{
    private:
        wxMutex * EvtMutex; //mutex used for constructing the condition
        wxCondition * EvtCondition; //used for signaling thread when to continue
        EventQueue * Queue; //A class which stores an array of events wrapped with a few mutexes
        void * ptrClient; //this is specific to my app, but its a pointer to the client which will handle the events
	public:
        virtual void * Entry();
        virtual void OnExit();
        EventHandler(EventQueue * Que, wxMutex* EvtMut, wxCondition* EvtCond, void * pClient);
};
EventHandler.cpp

Code: Select all

EventHandler::EventHandler(EventQueue * Que, wxMutex* EvtMut, wxCondition * EvtCond, void * pClient)
{
    ptrClient = pClient;
    Queue = Que;
    EvtMutex = EvtMut;
    EvtCondition = EvtCond;
}

void* EventHandler::Entry()
{
    while(true)
    {
        EvtMutex->Lock(); //lock the mutex this ensures that Broadcast cannot be called between now and Wait();
        if(Queue->GetCount() == 0) //Skip Wait() if there are more events to be processed
        {
            EvtCondition->Wait(); //If not wait till signaled
        } else { //Process events
            EvtMutex->Unlock(); //Unlock the mutex, after all the whole point is not to block the posting thread
            ClientEvent * evt = Queue->GetEvent();
            ((Client*)ptrClient)->EventParser(evt); //This points to my function which parses the actual event. Remember this is very simple :p
            delete evt;
        }
    } //loop back and do it again.
    return NULL;
}

void EventHandler::OnExit()
{
}
calling function, this is in a separate thread

Code: Select all

void Client::PostEvent(ClientEvent* evt)
{
    Queue->AddEvent(evt); //add the event to the queue
    //EvtMutex->Lock(); //Make sure we dont call signal between those two points or the event wont be handled
    EvtCondition->Broadcast();//Tell Wait to resume and process the events in the queue
}
This is a slightly different use of wxCondition than you may be use to. Instead of locking the mutex to ensure that Wait() is called on the condition, in this if the EventHandler is already processing events we dont want the posting thread to wait for Wait() to be called again, that would defeat the purpose of an event handler. Instead, mutex is only locked between when the Queue is checked to see if there are any events and as soon as either Wait() or Unlock() is called depending on if there are events in the queue.

Hope someone find this useful.
Last edited by *DEAD* on Fri Jan 18, 2008 11:21 am, edited 1 time in total.

*DEAD*
Experienced Solver
Experienced Solver
Posts: 60
Joined: Thu Jun 21, 2007 1:53 am

Post by *DEAD* » Fri Jan 18, 2008 11:20 am

I have to appologize, the EvtMutex->Lock() was added as an after thought and it doesnt work for some reason. Im not sure why yet, but if this is removed it will work. Ill post a fix if i can find one

Post Reply