Events to communicate between "threads" Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Tribune
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Nov 13, 2013 12:03 am

Events to communicate between "threads"

Post by Tribune »

Hello together,

I am faily new to wxWidgets and right now I am issuing a problem with a solution I am currently developing on.
basically the solution consists of 3 components:
1. the engine class (derived from wxApp)
2. a dialog class (wxDialog)
3. and a thread that does constant evaluations (inherits of wxThread of course ;-) )

Inside my thread there can occour two events that I'd like to give to my engine class for further processing (getting different information, showing up the dialog if needed and so on)...
I am now trying to get these events work for quite a while but until now with very little progress (most progress is on my understanding of wxwidgets but not enough to solve this on my own :-( ). I've tried several ways: with event tables, as well as the connect functions (I personally favor those). Most times I ended up with invalid static_casts to unresolved function types when trying to do connect or creating the event table.

The following is an example how I try to do it

Code: Select all

// This is my "controller", that starts a thread and shows a dialog if its needed 
class MyEngine : public wxApp
{
private:
    MyWXDialog* pMyDlg;
    MyThread* pMyThread;

public:
    MyEngine()
    {
        // These Connects do not compile (wxEventHandler tries to cast statically to an "unresolved overloaded function type")
        Connect(wxID_ANY, wxEVT_XXX, wxEventHandler(MyEngine::onThread1)), NULL, this);
        Connect(wxID_ANY, wxEVT_XXX, wxEventHandler(MyEngine::onThread2)), NULL, this);
    }
    virtual ~MyEngine();
    virtual bool OnInit()
    {
	pMyThread = new MyThread(this);
	pMyDlg = new MyWXDialog( (wxWindow*)NULL );
    	return true;
    }
    void onThread1(wxEvent& evt)
    {
        // for example...
        pMyDlg->Show();
    	SetTopWindow(pMyDlg);
    }
    void onThread2(wxEvent& evt)
    {}
};

//This is the thread that does constant evaluations. It shall inform MyEngine if "event" one or two has been found
class MyThread : public wxThread
{
    wxEvtHandler* pMain;
public:
    MyThread(wxEvtHandler* ref)
    {
        pMain = ref;
    }
protected:
    virtual ExitCode Entry()
    {
        while(!TestDestroy() )
	{
	    // When I find out that my first (second as well) event happened I need to tell that MyEngine to take further steps...
            wxCommandEvent event(wxEVT_XXX);
            pMain->AddPendingEvent(event);
        }
    }
};
I really hope one of the wx experts here can help me out. As I already mentioned the connect functions can not be compiled the way I implemented them. As well I am not sure if I implemented the events correctly (I always tried to do it the same way how various samples tryed to explain it to me)...
Maybe you even have a better idea how to do this. I am open to (and thankfull for) every possible solution.

Best Regards and thanks in advance
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Events to communicate between "threads"

Post by doublemax »

A class that wants to receive an event, must derive from wxEvtHandler. Just add wxEvtHandler as base class to MyEngine, and it should compile.

Code: Select all

Connect(wxID_ANY, wxEVT_XXX, wxEventHandler(MyEngine::onThread1)), NULL, this);
//
void onThread1(wxEvent& evt)
And you have to use "wxCommandEventHandler" resp. "wxCommandEvent" here.
Use the source, Luke!
Tribune
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Nov 13, 2013 12:03 am

Re: Events to communicate between "threads"

Post by Tribune »

Great, I got it running!

Thank you doublemax 8)

I even did not had to inherit from wxEvtHandler because my class already is child of wxApp.

As you advised me I used
"wxCommandEventHandler" resp. "wxCommandEvent"
everywhere. It took me about 20 minutes to hit all messed locations but finally it worked.
I think I just had messed up my code with events (from different tutorials) that could not work together (adding an wxEvent to handler and trying to cast it to wxCommandEvent or something else that is not right that way) :-)

Again: Thank you =D>
Regards
Post Reply