[Solved] UI events blocked the QLocalSocket 'readyRead' signal\slot



  • Hi All,

    • I'm using the QLocalSocket as an IPC to pass data from two applications (for the discussion: from writeApp to readApp).
    • writeApp write to the socket at ~10Hz (every 100 msec)
    • readApp read from the socket when readyRead emmited
    • readApp have a gui.

    Some code (The readApp is implemented with QThread, but it's not necessary, as explained below):
    writeApp.h:
    @#include <QLocalSocket>
    class writeApp: public QObject
    {
    Q_OBJECT

    public:
    writeApp(QString servername, QObject *parent = 0);

    private:
    QLocalSocket* m_socket;

    public slots:
    void onDataReady(QByteArray data);@

    writeApp.cpp:
    @writeApp::writeApp(QString servername, QObject *parent)
    {
    m_socket = new QLocalSocket(this);
    m_socket->connectToServer(m_serverName);
    }

    void writeApp::onDataReady(QByteArray data)
    {
    m_socket->write(data);
    m_socket->flush();
    }
    @

    readApp.h:
    @#include <QLocalServer>
    #include <QLocalSocket>
    #include <QThread>

    class readApp: public QThread
    {
    Q_OBJECT

    public:
    readApp(QString servername);

    protected:
    void run();

    public slots:
    void socket_new_connection();
    void onSocketReadReady();

    private:
    QLocalServer *m_server;
    QLocalSocket *clientConnection;@

    readApp.cpp:
    @readApp::readApp(QString servernameCanA, QString servernameCanB)
    {
    moveToThread(this);
    m_server = new QLocalServer(this);
    if (!m_server->listen(servername))
    qDebug() << "Not able to start the Server";
    }

    void readApp::run()
    {
    connect(m_server, SIGNAL(newConnection()), this, SLOT(socket_new_connection()));
    clientConnection = new QLocalSocket(this);
    exec();
    }

    void readApp::socket_new_connection()
    {
    clientConnection = m_server->nextPendingConnection();

    connect(clientConnection, SIGNAL(disconnected(&#41;),
            clientConnection, SLOT(deleteLater()));
    
    connect(clientConnection, SIGNAL(readyRead()),
            this, SLOT(onSocketReadReady()));
    

    }

    void readApp::onSocketReadReady()
    {
    QByteArray m_data = clientConnection->read(clientConnection->bytesAvailable());
    .
    .
    .
    }@

    Now,
    When i'm drag the window of the readApp, the readReady signal (or maybe the slot?) seem to be blocked (the program do not enter to the onSocketReadReady). Maybe by the windows events?
    (I'm tried to seperate the readApp to two threads, such that the socket reading will have another eventLoop from the GUI - but nothing seem to be changed...)

    Any ideas?
    Thanks for advance,
    Aviad



  • Your readApp implementation is a subclass of QThread and only the stuff that is done in the run() function is executed in another thread. Since the slots socket_new_Connection() and onSocketReadReady() belong to the QThread subclass readApp (which will live and work in the context of the thread were it was created) they are also running in the context of the main thread.

    The whole subclassing of QThread approach is not really recommended for your use case. Try to implement it using the "worker object aproach":http://qt-project.org/wiki/QThreads_general_usage.

    Edit: I just saw you did the old moveToThread(this) trick in the subclasses constructor. Have you already read the "You're doing it wrong":http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ article?



  • Yhea! Its work!
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.