This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threaddao.cpp
79 lines (66 loc) · 1.61 KB
/
threaddao.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <QDebug>
#include <QEventLoop>
#include <QApplication>
#include <QPushButton>
#include "threaddao.h"
#include "sleeperthread.h"
const int SleepTimeMs = 10;
const int TooLong = 100;
ThreadDAO::ThreadDAO(QProgressIndicator &progressIndicator, QObject *parent) :
QThread(parent),
m_haveFunction(false),
m_callFinished(false),
m_stop(false),
m_progressindicator(progressIndicator)
{
}
void ThreadDAO::run()
{
qDebug() << "ThreadDAO::run";
while (!m_stop)
{
while (!m_haveFunction)
{
SleeperThread::msSleep(SleepTimeMs);
}
m_callFinished = false;
m_lambda();
m_haveFunction = false;
m_callFinished = true;
}
}
void ThreadDAO::callSync(func lambda, QPushButton& btn)
{
setLambda(lambda);
bool animationIsShowed = false;
int waitCycles = 0;
while (m_haveFunction && !m_callFinished) {
qApp->processEvents( QEventLoop::AllEvents );
SleeperThread::msSleep(SleepTimeMs);
++waitCycles;
if (waitCycles > TooLong && !animationIsShowed)
{
qDebug() << "Start animation";
btn.setEnabled(false);
m_progressindicator.startAnimation();
animationIsShowed = true;
}
}
if (animationIsShowed)
{
qDebug() << "Stop animation";
btn.setEnabled(true);
m_progressindicator.stopAnimation();
}
}
void ThreadDAO::stopThread()
{
qDebug() << "Stopping thread";
m_stop = true;
}
void ThreadDAO::setLambda(func lambda)
{
m_lambda = lambda;
m_haveFunction = true;
m_callFinished = false;
}