Browse Source

Stop GUI threads gracefully

Jonas Pflanzer 5 years ago
parent
commit
15fdae7bd8
1 changed files with 15 additions and 3 deletions
  1. 15 3
      gui/src/climanager.cpp

+ 15 - 3
gui/src/climanager.cpp

@@ -25,7 +25,11 @@
 
 using namespace std;
 
+thread readPipeLoopThread;
+thread notificationsLoopThread;
+
 namespace CliManager {
+
 QMLHandler *qmlHandler;
 bool programActive = true;
 int inpipefd[2];
@@ -41,7 +45,14 @@ void CliManager::writeToCli(QString command) {
 	write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
 }
 
-void CliManager::onExit() { writeToCli("exit"); }
+void CliManager::onExit() {
+	// stop threads
+	CliManager::setProgramActive(false);
+	readPipeLoopThread.join();
+	notificationsLoopThread.join();
+
+	writeToCli("exit");
+}
 
 void CliManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
 
@@ -67,8 +78,9 @@ void CliManager::init() {
 	close(outpipefd[0]);
 	close(inpipefd[1]);
 
-	std::thread(&CliManager::readPipeLoop).detach();
-	std::thread(&CliManager::notificationsLoop).detach();
+	// start threads
+	readPipeLoopThread = thread(&CliManager::readPipeLoop);
+	notificationsLoopThread = thread(&CliManager::notificationsLoop);
 }
 
 std::vector<std::string> tokenizeByNewlines(std::string in) {