简体   繁体   English

wxWidgets-wxThread

[英]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: 线程的类MyFrame处理程序:

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. 现在的问题是,当客户端连接到我的应用程序时,侦听线程会收到此传入的连接(显然,对于每个连接,它使用的是与sockConvs数组不同的SocketBase)并分配(如您所见)新的MyFrame。 BUT! 但! at the end of the Entry method my new Frame is closed. 在Entry方法的末尾,我的新Frame已关闭。 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 . 我怀疑这可能与您不应该在辅助线程中进行任何GUI调用有关,这在wxThread文档中有更全面的说明。 You probably ought to post an event back to your main thread and create the frame there. 您可能应该将事件发回到主线程并在其中创建框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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