代码之家  ›  专栏  ›  技术社区  ›  The Man

QThread:线程仍在运行时已销毁?

  •  19
  • The Man  · 技术社区  · 10 年前

    我想开始我的 QThread 当我按下按钮时 。但编译器输出以下错误:

    QThread: Destroyed while thread is still running
    ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cp.
    

    我不知道我的代码有什么问题。

    如有任何帮助,将不胜感激。

    这是我的代码:

    SamplingThread::SamplingThread( QObject *parent):
       QwtSamplingThread( parent ),
       d_frequency( 5.0 )
    {
       init();
    }
    
    MainWindow::MainWindow( QWidget *parent ):
    QMainWindow( parent )
    {.......
      .....
       run= new QPushButton ("Run",this);
       stop= new QPushButton("Stop",this);
       connect(run, SIGNAL(clicked()),this, SLOT (start()));
    }
    
    MainWindow::start
    {
       SamplingThread samplingThread;
       samplingThread.setFrequency( frequency() );
       samplingThread.start();
    }
    
    int main( int argc, char **argv )
    {
       QApplication app( argc, argv );
       MainWindow window;
       window.resize( 700, 400 );
       window.show();
       bool ok = app.exec();
       return ok;
    }
    
    3 回复  |  直到 10 年前
        1
  •  35
  •   Erik    10 年前

    如错误消息所示: QThread: Destroyed while thread is still running 。您正在创建 SamplingThread 对象 MainWindow::start 方法,但当该方法终止时,它超出范围(即被销毁)。我认为有两种简单的方法:

    1. 你让你的 采样线程 您的成员 MainWindow 因此其寿命与 主窗口 例子
    2. 使用指针,即创建 采样线程 使用

      SamplingThread *samplingThread = new SamplingThread;

    这有帮助吗?

    编辑:为了说明这两种情况,用一个非常粗糙的例子来说明这两个情况

    #include <iostream>
    #include <QApplication>
    #include <QThread>
    
    class Dummy
    {
    public:
      Dummy();
      void start();
    private:
      QThread a;
    };
    
    Dummy::Dummy() :
      a()
    {
    }
    
    
    void Dummy::start()
    {
      a.start();
      QThread *b = new QThread;
      b->start();
    
      if( a.isRunning() ) {
        std::cout << "Thread a is running" << std::endl;
      }
      if( b->isRunning() ) {
        std::cout << "Thread b is running" << std::endl;
      }
    }
    
    int main(int argc, char** argv)
    {
      QApplication app(argc,argv);
      Dummy d;
      d.start();
      return app.exec();
    }
    
        2
  •  5
  •   Marek R    10 年前

    这是C++的基础知识!您正在创建的本地对象 QThread 在堆栈上而不是在堆上,因此当您离开方法时,它会立即被销毁 MainWindow::start .

    应该这样做:

    MainWindow::MainWindow( QWidget *parent ):
    QMainWindow( parent )
    {
       ...
    
       samplingThread = SamplingThread(this);
       samplingThread->setFrequency( frequency() );
    
       run= new QPushButton ("Run",this);
       stop= new QPushButton("Stop",this);
       connect(run, SIGNAL(clicked()), samplingThread, SLOT(start()));
    }
    
    MainWindow::~MainWindow() {
       samplingThread->waitFor(5000);
    }
    
        3
  •  3
  •   Ulrich Eckhardt    10 年前

    涉及两个不同的“线程”:一个是实际的线程,另一个是表示它的C++对象(正确地说,这段代码最初是从另一个线程开始的)。

    错误只是说线程在表示它的C++对象被破坏的地方仍然在运行。在代码中,原因是QThread实例是本地的 start() 。也许您想将QThread存储在成员中。