From 49bb174c8264545c61b3315ef8f2785da71a2c70 Mon Sep 17 00:00:00 2001
From: leeffo <leeffo@yeah.net>
Date: Thu, 9 Feb 2023 15:33:43 +0800
Subject: [PATCH] feat: rewrite loadDeviceInfo updateDeviceInfo

---
 .../src/LoadInfo/ThreadPool.cpp               | 58 ++++++++++++++++---
 1 file changed, 50 insertions(+), 8 deletions(-)

diff --git a/deepin-devicemanager-server/src/LoadInfo/ThreadPool.cpp b/deepin-devicemanager-server/src/LoadInfo/ThreadPool.cpp
index 911c4ee..6c358ce 100644
--- a/deepin-devicemanager-server/src/LoadInfo/ThreadPool.cpp
+++ b/deepin-devicemanager-server/src/LoadInfo/ThreadPool.cpp
@@ -6,6 +6,7 @@
 #include <QProcess>
 #include <QDir>
 #include <QDebug>
+#include <QDateTime>
 
 ThreadPool::ThreadPool(QObject *parent)
     : QThreadPool(parent)
@@ -16,31 +17,72 @@ ThreadPool::ThreadPool(QObject *parent)
     dir.mkdir(PATH);
 }
 
+//void ThreadPool::loadDeviceInfo()
+//{
+//    // 根据m_ListCmd生成所有设备信息
+//    QObjectCleanupHandler *cleaner = new QObjectCleanupHandler;
+//    cleaner->setParent(this);
+//    QList<Cmd>::iterator it = m_ListCmd.begin();
+//    for (; it != m_ListCmd.end(); ++it) {
+//        ThreadPoolTask *task = new ThreadPoolTask((*it).cmd, (*it).file, (*it).canNotReplace, (*it).waitingTime);
+//        cleaner->add(task);
+//        start(task);
+//        task->setAutoDelete(true);
+//    }
+//}
+//
+//void ThreadPool::updateDeviceInfo()
+//{
+//    // 根据m_ListCmd生成所有设备信息
+//    QObjectCleanupHandler *cleaner = new QObjectCleanupHandler;
+//    cleaner->setParent(this);
+//    QList<Cmd>::iterator it = m_ListUpdate.begin();
+//    for (; it != m_ListUpdate.end(); ++it) {
+//        ThreadPoolTask *task = new ThreadPoolTask((*it).cmd, (*it).file, (*it).canNotReplace, (*it).waitingTime);
+//        cleaner->add(task);
+//        start(task);
+//        task->setAutoDelete(true);
+//    }
+//}
+
 void ThreadPool::loadDeviceInfo()
 {
     // 根据m_ListCmd生成所有设备信息
-    QObjectCleanupHandler *cleaner = new QObjectCleanupHandler;
-    cleaner->setParent(this);
     QList<Cmd>::iterator it = m_ListCmd.begin();
     for (; it != m_ListCmd.end(); ++it) {
         ThreadPoolTask *task = new ThreadPoolTask((*it).cmd, (*it).file, (*it).canNotReplace, (*it).waitingTime);
-        cleaner->add(task);
-        start(task);
         task->setAutoDelete(true);
+        start(task);
+    }
+
+    // 当所有设备执行完毕之后，开始执行生成其它设备的任务
+    // 这里是为了确保其它设备在最后一个生成
+    qint64 beginMSecond = QDateTime::currentMSecsSinceEpoch();
+    while (true) {
+        qint64 curMSecond = QDateTime::currentMSecsSinceEpoch();
+        if (activeThreadCount() == 0 || curMSecond - beginMSecond > 10000) {
+            break;
+        }
     }
 }
 
 void ThreadPool::updateDeviceInfo()
 {
     // 根据m_ListCmd生成所有设备信息
-    QObjectCleanupHandler *cleaner = new QObjectCleanupHandler;
-    cleaner->setParent(this);
     QList<Cmd>::iterator it = m_ListUpdate.begin();
     for (; it != m_ListUpdate.end(); ++it) {
         ThreadPoolTask *task = new ThreadPoolTask((*it).cmd, (*it).file, (*it).canNotReplace, (*it).waitingTime);
-        cleaner->add(task);
-        start(task);
         task->setAutoDelete(true);
+        start(task);
+    }
+    // 当所有设备执行完毕之后，开始执行生成其它设备的任务
+    // 这里是为了确保其它设备在最后一个生成
+    qint64 beginMSecond = QDateTime::currentMSecsSinceEpoch();
+    while (true) {
+        qint64 curMSecond = QDateTime::currentMSecsSinceEpoch();
+        if (activeThreadCount() == 0 || curMSecond - beginMSecond > 10000) {
+            break;
+        }
     }
 }
 
-- 
2.20.1
