线程未在继承类中启动

时间:2019-04-23 16:38:57

标签: qt qthread

我有申请来接收不同协议串行以太网的值... 我有协议列表,我有成员qThread。

在此线程中,我有本地对象串行,以太网,对象通信在线程中。

ex类:serialThread

  serialThread::serialThread(Protocole *Parent): ThreadProt( this,       Parent )
   {
    qDebug() << "new serial thread:" << this;
    setParent(Parent); //parent = protocole
   }

运行在这里实现

    void serialThread::run()
    {

      qDebug() << "thread serial:" << QThread::currentThreadId();

      serial l_serial(this,m_prot);

       exec();
    }

Class ThreadProt:公共QThread 我创建了threadProt来为不同协议的代码分组。

使用deleteLater(serialThread)杀死线程杀死父对象

线程信号

在run()线程中访问本地对象

    ThreadProt::ThreadProt(QObject*pParent,Protocole* pProt)
    {
     qDebug() << "new threadProt:" << this;
     setParent(pParent); //serialthread        Modbus thread
     m_prot = pProt;
     m_o_thread = NULL;
     connect(this, SIGNAL(finished())   ,pParent  , SLOT(deleteLater())   );
     //qthread fini on détruit le parent
     start();
     }

有时线程不是start(),而不是在串行线程中执行run()。

为什么不是继承qthread的解决方案?

我已经使用此结构重新启动了我的代码。 我更喜欢不使用move to thread,而将我的对象放置在thread的本地。

我认为更好,但是如果您有Intermediaire类,则此设计无法正常工作

好的,我不知道threadProt是否必须使用setParent。

在QML的Q_INVOKABLE dashBoardStart()函数中通过调用启动线程startProt()。

  void App::dashBoardStart()
  {
    quint32 l_prot;


    qDebug() << "start dashboard" << LstProt.length();

    if (LstProt.count() ==0)
     return;

    for (l_prot=0;l_prot < LstProt.count();l_prot++)
    {
      Protocole* l_pt =  LstProt[l_prot].value<Protocole*>();

       if (m_lst_prot_ready)
         m_lst_prot_ready->append(false);


       l_pt->startProt();
     }//Finsi
    }

    bool Protocole::startProt()
    {

     //allocation du thread serial
     if (m_liaison.ConfigRs == NULL)
       m_liaison.ConfigRs = new serialThread(this);

      return (l_ret);
    }

有什么主意,因为如果在继承类中被调用,线程不会启动?

thread isRunning = true isStarted是调用的,但在这种情况下不是调用run()。

0 个答案:

没有答案