开发者

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 My开发者_如何转开发Frame 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜