简体   繁体   中英

wxWidgets - wxThread

I'm developing an application which needs to have a permanent server socket listening, so I put it in a thread, here there is the class I've written for it:

class listenThread : public wxThread
{
    public:
        listenThread(MyFrame *h) : wxThread() { handler = h; };
        virtual void * Entry();
    private:
        MyFrame *handler;
};

void *listenThread::Entry()
{
    handler->sockConvs[nconvs] = handler->sockServer->Accept();

    if(handler->sockConvs[handler->nconvs]->IsConnected() && handler->nconvs < 10)
    {
    handler->frames[handler->nconvs] = new MyFrame(NULL);
    handler->frames[handler->nconvs++]->Show();
    }
}

The class MyFrame handler of Thread:

class MyFrame : public wxFrame
{
    friend class listenThread;

    public:

           /* other stuff ... */

    private:
           /* other stuff ... */

    private:
        listenThread *myThread;

        // Both initialized in MyFrame class constructor
        wxSocketServer *sockServer;
        wxIPV4address addr;

        wxSocketBase *sockConvs[10];
        MyFrame *frames[10];
        int nconvs;
};

Now the problem is that when a client connects to my Application, the listening thread receives this incoming connection (for each connection, obiviusly, it uses a different SocketBase from sockConvs array) and allocates (as you can see) a new MyFrame. BUT! at the end of the Entry method my new Frame is closed. Why?

Thank you!

I suspect this could be related to the fact that you are not supposed to make any GUI calls in secondary threads, this is explained more fully in the documentation of wxThread . You probably ought to post an event back to your main thread and create the frame there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM