QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)
注意:本次实现线程的暂停执行主要采用互斥量的方法,如果有更好的实现方法的小伙伴可以在下面留言!
直接插入代码了,由于做的小demo,代码写的可能有点乱,但还算完整。
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 |

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 }


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


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("继续"));
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 }

还有一个简单的ui界面就不贴了,就三个button
暂停、继续就可以实现了,但很奇怪的事情是当将线程terminate 后再调用start 也会好像出现“暂停、继续”的效果,这个正在思考中,如果小伙伴有知道的留言tell me啊!
QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)的更多相关文章
- C++11消息队列 + Qt线程池 + QRunnable执行任务简单模型
1.模板类queue,包含头文件<queue>中,是一个FIFO队列. queue.push():在队列尾巴增加数据 queue.pop():移除队列头部数据 queue.font():获 ...
- Qt中暂停线程的执行(主线程和工作线程共用一把锁,一旦主线程将它锁上,工作线程就无法运行了,这也是一个办法)
在线程中定义一个信号量: QMutex pause; 把run()函数中循环执行的部分用信号量pause锁住: void run() { while(1) { pause.lock ...
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- 三个线程T1,T2,T3.保证顺序执行的三种方法
经常看见面试题:有三个线程T1,T2,T3,有什么方法可以确保它们按顺序执行.今天手写测试了一下,下面贴出目前想到的3种实现方式 说明:这里在线程中我都用到了sleep方法,目的是更容易发现问题.之前 ...
- Qt 控制线程的顺序执行(使用QWaitCondition,并且线程类的run函数里记得加exec(),使得线程常驻)
背景项目中用到多线程,对线程的执行顺序有要求: A.一个线程先收数据 B.一个线程处理数据 C.一个线程再将处理后的数据发送出去 要求三个线程按照ABC的顺序循环执行. 思路子类化多线程方法 重写子类 ...
- Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别? 线程的sleep()方法和yield()方法有什么区别?
Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别? sleep()方法(休眠)是线程类(Thread)的静态方法,调用此方法会让当前线程暂停执行指定的时间 ...
- Qt线程(1) moveToThread
若在Qt准备使用线程类一般有两种方式(1) 采用WorkObject配合QThread进行使用 (2)继承QThread, 重载run()函数即可. 注:采用Qt::Concurrent之类的不在本文 ...
- QT核心编程之Qt线程 (c)
QT核心编程之Qt线程是本节要介绍的内容,QT核心编程我们要分几个部分来介绍,想参考更多内容,请看末尾的编辑推荐进行详细阅读,先来看本篇内容. Qt对线程提供了支持,它引入了一些基本与平台无关的线程类 ...
- Qt 学习之路 :Qt 线程相关类
希望上一章有关事件循环的内容还没有把你绕晕.本章将重新回到有关线程的相关内容上面来.在前面的章节我们了解了有关QThread类的简单使用.不过,Qt 提供的有关线程的类可不那么简单,否则的话我们也没必 ...
随机推荐
- hibernate批量删除和更新数据
转载自:http://blog.csdn.net/yuhua3272004/article/details/2909538 Hibernate3.0 採用新的基于ANTLR的HQL/SQL查询翻译器, ...
- 在Ubuntu 12.10 上安装部署Openstack
OpenStack系统有几个关键的项目,它们能够独立地安装但是能够在你的云计算中共同工作.这些项目包括:OpenStack Compute,OpenStack Object Storage,OpenS ...
- windows 与Linux 互传文件
下载putty,将putty的安装路径添加到Windows的环境变量中: 我的电脑->属性->高级->环境变量->系统变量,双击其中的Path,在分号后添加putty的 ...
- ip、数字的互转
# ip ==> 数字 >>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[: ...
- node.js中文资料导航
以下资料来自gitHUb上面:https://github.com/youyudehexie/node123 Node.js HomePage Node官网七牛镜像 Infoq深入浅出Node.js系 ...
- PHP 的try catch 报错捕获机制
首先上代码: try { echo 'Never executed'; echo "<br>"; if(1<0){ echo 'end'; }else{ thro ...
- java异常类的使用
1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...
- jquery生成UUID的方法
来源: http://www.broofa.com/2008/09/javascript-uuid-function/ 1.代码: http://www.broofa.com/Tools/Math ...
- 解决右滑返回手势和UIScrollView中的手势冲突
当在一个viewController中添加了scrollView或者tableView的时候,贴边侧滑返回的时候会首先触发滚动而失效,要解决这个问题,需要通过requireGestureRecogni ...
- 极端气候频现 五款开发天气预报应用的API
http://www.csdn.net/article/2014-02-07/2818322-weather-forecast-api-for-developing-apps