Skip to content

Commit 027c31f

Browse files
committed
GUI: Implement stopping the compare.
Threads must be exited from check cleanly even though it takes small amount of time. Just terminating thread can have unpredictable side-effects (even weird crashes).
1 parent e791d1c commit 027c31f

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

gui/checkthread.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,30 @@ void CheckThread::Check(Settings settings)
4040

4141
void CheckThread::run()
4242
{
43+
mState = Running;
4344
QString file;
4445
file = mResult.GetNextFile();
4546

46-
while (!file.isEmpty())
47+
while (!file.isEmpty() && mState == Running)
4748
{
4849
qDebug() << "Checking file" << file;
4950
mCppcheck.addFile(file.toStdString());
5051
mCppcheck.check();
5152
mCppcheck.clearFiles();
5253
emit FileChecked(file);
5354

54-
file = mResult.GetNextFile();
55+
if (mState == Running)
56+
file = mResult.GetNextFile();
5557
}
58+
if (mState == Running)
59+
mState = Ready;
60+
else
61+
mState = Stopped;
5662

5763
emit Done();
5864
}
5965

60-
61-
66+
void CheckThread::stop()
67+
{
68+
mState = Stopping;
69+
}

gui/checkthread.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class CheckThread : public QThread
4949
*/
5050
void run();
5151

52+
void stop();
53+
5254

5355
signals:
5456

@@ -60,6 +62,27 @@ class CheckThread : public QThread
6062

6163
void FileChecked(const QString &file);
6264
protected:
65+
66+
/**
67+
* @brief States for the check thread.
68+
* Whole purpose of these states is to allow stopping of the checking. When
69+
* stopping we say for the thread (Stopping) that "stop when current check
70+
* has been completed. Thread must be stopped cleanly, just terminating thread
71+
* likely causes unpredictable side-effedts.
72+
*/
73+
enum State
74+
{
75+
Running, /**< The thread is checking. */
76+
Stopping, /**< The thread will stop after current work. */
77+
Stopped, /**< The thread has been stopped. */
78+
Ready, /**< The thread is ready. */
79+
};
80+
81+
/**
82+
* @brief Thread's current execution state.
83+
*/
84+
State mState;
85+
6386
ThreadResult &mResult;
6487
/**
6588
* @brief Cppcheck itself

gui/mainwindow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ MainWindow::MainWindow() :
105105

106106
connect(&mActionReCheck, SIGNAL(triggered()), this, SLOT(ReCheck()));
107107

108-
//TODO: This crashed
109-
//connect(&mActionStop, SIGNAL(triggered()), &mThread, SLOT(Stop()));
108+
connect(&mActionStop, SIGNAL(triggered()), &mThread, SLOT(Stop()));
110109
connect(&mActionSave, SIGNAL(triggered()), this, SLOT(Save()));
111110

112111
connect(&mActionAbout, SIGNAL(triggered()), this, SLOT(About()));

gui/threadhandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void ThreadHandler::Stop()
124124
{
125125
for (int i = 0;i < mThreads.size();i++)
126126
{
127-
mThreads[i]->terminate();
127+
mThreads[i]->stop();
128128
}
129129
}
130130

0 commit comments

Comments
 (0)