Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Timers cannot be ####### from another thread
Forum Updated to NodeBB v4.3 + New Features

Timers cannot be ####### from another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 4 Posters 5.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    @SPlatten move the socket creation in run. Sockets cannot be moved to other threads. They have to be created in the thread they will actually work in.

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #18

    @SGaist, @eyllanesc , do you mean:

    void clsMsgSender::run() {    
        mpsckClient = new QTcpSocket(this);
        QObject::connect(mpsckClient, &QTcpSocket::connected
                        ,this, &clsMsgSender::onConnected);
        QObject::connect(mpsckClient, &QAbstractSocket::errorOccurred
                        ,this, &clsMsgSender::onErrorOccurred);
        QObject::connect(mpsckClient, &QTcpSocket::disconnected
                        ,this, &clsMsgSender::onDisconnected);
        //Default last state to connected as it won't be!
        QTcpSocket::SocketState sckCurState;
        while( mblnRun == true  ) {
    ...
    

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @eyllanesc , mpsckClient is created in the clsMsgSender constructor which is not in a thread.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #19

      @SPlatten said in Timers cannot be ####### from another thread:

      @eyllanesc , mpsckClient is created in the clsMsgSender constructor which is not in a thread.

      Wrong, the object creation happens in the main thread.

      You always have at least one thread when you execute an application.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS 1 Reply Last reply
      1
      • SGaistS SGaist

        @SPlatten said in Timers cannot be ####### from another thread:

        @eyllanesc , mpsckClient is created in the clsMsgSender constructor which is not in a thread.

        Wrong, the object creation happens in the main thread.

        You always have at least one thread when you execute an application.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #20

        @SGaist , then that is exactly how it was done already since the object clsMsgSender is created in the main thread.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #21

          And that is the issue. You are thus creating your socket in the main thread as well.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          SPlattenS 1 Reply Last reply
          0
          • SGaistS SGaist

            And that is the issue. You are thus creating your socket in the main thread as well.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #22

            @SGaist , sorry, I'm not sure why you say that, the class clsMsgSender is not a socket. If you mean that:

            mpsckClient = new QTcpSocket(this);
            

            Is that the problem? because this is in the clsMsgSender constructor, then please explain what I should do because I suggested moving it into the thread and the response was no.

            Can you please clarify what I'm doing wrong and how to fix it?

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #23

              @SPlatten said in Timers cannot be ####### from another thread:

              new QTcpSocket(this);

              This creates an object with the parent in the main thread. And since a child must be in the same thread as the parent...
              Don't pass this as parent and delete it by your own.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #24

                @Christian-Ehrlicher for sockets you have to go one step further and ensure it's created in the thread that will actually use it.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                SPlattenS 1 Reply Last reply
                1
                • SGaistS SGaist

                  @Christian-Ehrlicher for sockets you have to go one step further and ensure it's created in the thread that will actually use it.

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #25

                  Obviously I have a lot to learn where Qt threads are concerned, can someone point me to some relevant thread & socket tutorials or documentation?

                  I will take a closer look at fortuneserver and fortuneclient.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #26

                    Start with the QThread. There are several other chapters linked from the details of the class.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    SPlattenS 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Start with the QThread. There are several other chapters linked from the details of the class.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #27

                      @SGaist , I've just read the details on QThread and I didn't see anything specifically regarding the use of Sockets. I then found this when searching online:

                      https://forum.qt.io/topic/57753/qtcpsocket-and-qthread
                      Obviously you are familiar with this one.

                      https://stackoverflow.com/questions/34641732/qtcpsocket-handling-in-another-qthread
                      Not sure what the conclusion of the above was...

                      https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
                      Working through this....

                      I must say the approach the last link takes is exactly the way I started, with sub-classing my listening class from QTcpServer, however after several posts here advising not to do this I changed my class.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved