乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      QT 線程暫停,繼續(xù)執(zhí)行的一種實(shí)現(xiàn)

       tianht 2015-07-13
      轉(zhuǎn):http://www.cnblogs.com/liu-hq/p/4281806.html

      注意:本次實(shí)現(xiàn)線程的暫停執(zhí)行主要采用互斥量的方法,如果有更好的實(shí)現(xiàn)方法的小伙伴可以在下面留言!

      直接插入代碼了,由于做的小demo,代碼寫的可能有點(diǎn)亂,但還算完整。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      //mythread.h
      #ifndef MYTHREAD_H
      #define MYTHREAD_H
      #include <QDebug>
      #include <QThread>
      #include <QMutex>
      class MyThread:public QThread
      {
          Q_OBJECT
      public:
          MyThread();
          ~MyThread();
          void run();
      public slots:
          void threadStart();
          void threadPause();
          void threadStop();
          void threadResume();
          void threadPR();
      private:
          bool m_buttonState; //if pause m_buttonState=false;else m_buttonState=true;
          int m_i;
          QMutex m_mutex;//互斥量
      };
       
      #endif // MYTHREAD_H

       

      復(fù)制代碼
       1 //mythread.cpp
       2 #include "mythread.h"
       3 MyThread::MyThread()
       4 {
       5     m_i=0;
       6     m_buttonState=false;
       7 }
       8 
       9 MyThread::~MyThread()
      10 {
      11 
      12 }
      13 
      14 void MyThread::run()
      15 {
      16     m_buttonState=true;
      17     while(1)
      18     {
      19         m_mutex.lock();
      20         m_i++;
      21         qDebug()<<QString("the value of m_i is %1 ").arg(m_i);
      22         m_mutex.unlock();
      23         this->sleep(1);
      24     }
      25 
      26 
      27 
      28 }
      29 
      30 
      31 void MyThread::threadPause()
      32 {
      33     qDebug()<<QString("pause :%1").arg(m_buttonState);
      34         this->m_mutex.lock();
      35         this->m_buttonState=false;
      36         qDebug()<<QString("pause");
      37 }
      38 void MyThread::threadResume()
      39 {
      40       qDebug()<<QString("resume :%1").arg(m_buttonState);
      41         this->m_mutex.unlock();
      42         this->m_buttonState=true;
      43         qDebug()<<QString("resume");
      44 
      45 }
      46 void MyThread::threadStop()
      47 {
      48     this->exit();
      49 
      50 }
      51 void MyThread::threadStart()
      52 {
      53     this->start();
      54 }
      55 void MyThread::threadPR()
      56 {
      57     if(m_buttonState)
      58     {
      59         threadPause();
      60 
      61     }
      62     else
      63     {
      64         threadResume();
      65     }
      66 
      67 }
      復(fù)制代碼
      復(fù)制代碼
       1 //mainwindow.h
       2 #ifndef MAINWINDOW_H
       3 #define MAINWINDOW_H
       4 
       5 #include <QMainWindow>
       6 #include "mythread.h"
       7 namespace Ui {
       8 class MainWindow;
       9 }
      10 
      11 class MainWindow : public QMainWindow
      12 {
      13     Q_OBJECT
      14 
      15 public:
      16     explicit MainWindow(QWidget *parent = 0);
      17     ~MainWindow();
      18 private slots:
      19     void changeButton();
      20     void threadStop();
      21     void threadStart();
      22     void threadPR();
      23 private:
      24     Ui::MainWindow *ui;
      25     MyThread *myThread;
      26     MyThread *oneThread;
      27 
      28 };
      29 
      30 #endif // MAINWINDOW_H
      復(fù)制代碼
      復(fù)制代碼
       1 //mainwindow.cpp
       2 #include "mainwindow.h"
       3 #include "ui_mainwindow.h"
       4 
       5 MainWindow::MainWindow(QWidget *parent) :
       6     QMainWindow(parent),
       7     ui(new Ui::MainWindow)
       8 {
       9     ui->setupUi(this);
      10     myThread=new MyThread;
      11     oneThread=new MyThread;
      12     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(changeButton()));
      13     connect(ui->startButton,SIGNAL(clicked()),this,SLOT(threadStart()));
      14     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(threadPR()));
      15     connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(threadStop()));
      16 }
      17 
      18 MainWindow::~MainWindow()
      19 {
      20     if(ui!=NULL)
      21     {
      22         delete ui;
      23         ui=NULL;
      24     }
      25     if(myThread!=NULL)
      26     {
      27         delete myThread;
      28         myThread=NULL;
      29     }
      30     if(oneThread!=NULL)
      31     {
      32         delete oneThread;
      33         oneThread=NULL;
      34     }
      35 }
      36 
      37 void MainWindow::changeButton()
      38 {
      39 
      40 
      41     if(!QString::compare(ui->pauseButton->text(),QString::fromUtf8("暫停")))
      42     {
      43         ui->pauseButton->setText(QString::fromUtf8("繼續(xù)"));
      44     }else
      45     {
      46         ui->pauseButton->setText(QString::fromUtf8("暫停"));
      47     }
      48 }
      49 
      50 void MainWindow::threadStart()
      51 {
      52     myThread->threadStart();
      53     oneThread->threadStart();
      54 
      55 }
      56 void MainWindow::threadStop()
      57 {
      58     myThread->terminate();
      59     oneThread->terminate();
      60 
      61 }
      62 void MainWindow::threadPR()
      63 {
      64     myThread->threadPR();
      65     oneThread->threadPR();
      66 
      67 
      68 }
      復(fù)制代碼

      還有一個(gè)簡單的ui界面就不貼了,就三個(gè)button

      暫停、繼續(xù)就可以實(shí)現(xiàn)了,但很奇怪的事情是當(dāng)將線程terminate 后再調(diào)用start 也會好像出現(xiàn)“暫停、繼續(xù)”的效果,這個(gè)正在思考中,如果小伙伴有知道的留言tell me??!


        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多