1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-18 07:23:03 +08:00

fix(ALWebDriverDownloadDialog): 重命名信号避免与 QThread 内置信号冲突并改进线程生命周期管理

This commit is contained in:
2026-03-21 01:53:22 +08:00
parent 4924f4b031
commit 5c393595d7
+35 -17
View File
@@ -35,9 +35,9 @@ class DownloadWorker(QThread):
""" """
progress = Signal(float, int, float, str) progress = Signal(float, int, float, str)
finished = Signal(object, str) downloadFinished = Signal(object, str)
error = Signal(str) downloadError = Signal(str)
cancelled = Signal() downloadCancelled = Signal()
def __init__( def __init__(
self, self,
@@ -66,7 +66,7 @@ class DownloadWorker(QThread):
): ):
try: try:
if self.__cancelled: if self.__cancelled:
self.cancelled.emit() self.downloadCancelled.emit()
return return
self.__driver_path = self.__driver_manager.installDriver( self.__driver_path = self.__driver_manager.installDriver(
self.__driver_info, self.__driver_info,
@@ -74,15 +74,15 @@ class DownloadWorker(QThread):
cancel_event=self.__cancel_event cancel_event=self.__cancel_event
) )
if self.__cancelled: if self.__cancelled:
self.cancelled.emit() self.downloadCancelled.emit()
return return
if self.__driver_path: if self.__driver_path:
self.finished.emit(self.__driver_path, "") self.downloadFinished.emit(self.__driver_path, "")
else: else:
self.error.emit("下载失败: 未返回有效路径") self.downloadError.emit("下载失败: 未返回有效路径")
except Exception as e: except Exception as e:
if not self.__cancelled: if not self.__cancelled:
self.error.emit(f"下载失败: {str(e)}") self.downloadError.emit(f"下载失败: {str(e)}")
def onProgress( def onProgress(
self, self,
@@ -97,6 +97,20 @@ class DownloadWorker(QThread):
if not self.__cancelled: if not self.__cancelled:
self.progress.emit(downloaded, total, speed, message) self.progress.emit(downloaded, total, speed, message)
def stop(
self
):
"""
Cancel and wait for the thread to finish.
Must only be called from the main thread.
"""
self.cancel()
if not self.isFinished():
if not self.wait(5000):
self.terminate()
self.wait()
class ALWebDriverDownloadDialog(QDialog): class ALWebDriverDownloadDialog(QDialog):
@@ -328,9 +342,10 @@ class ALWebDriverDownloadDialog(QDialog):
self.ProgressText.setText("正在下载驱动...") self.ProgressText.setText("正在下载驱动...")
self.__download_thread = DownloadWorker(self.__driver_manager, driver_info) self.__download_thread = DownloadWorker(self.__driver_manager, driver_info)
self.__download_thread.progress.connect(self.onDownloadProgress) self.__download_thread.progress.connect(self.onDownloadProgress)
self.__download_thread.finished.connect(self.onDownloadFinished) self.__download_thread.downloadFinished.connect(self.onDownloadFinished)
self.__download_thread.error.connect(self.onDownloadError) self.__download_thread.downloadError.connect(self.onDownloadError)
self.__download_thread.cancelled.connect(self.onDownloadCancelled) self.__download_thread.downloadCancelled.connect(self.onDownloadCancelled)
self.__download_thread.finished.connect(self.__onThreadFinished)
self.__download_thread.start() self.__download_thread.start()
@Slot() @Slot()
@@ -363,7 +378,6 @@ class ALWebDriverDownloadDialog(QDialog):
if 0 <= index < len(self.__driver_infos): if 0 <= index < len(self.__driver_infos):
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
self.updateDriverInfoDisplay(driver_info) self.updateDriverInfoDisplay(driver_info)
self.__download_thread = None
self.ConfirmButton.setEnabled(True) self.ConfirmButton.setEnabled(True)
self.DownloadButton.setEnabled(False) self.DownloadButton.setEnabled(False)
self.RefreshButton.setEnabled(True) self.RefreshButton.setEnabled(True)
@@ -389,9 +403,6 @@ class ALWebDriverDownloadDialog(QDialog):
self self
): ):
if self.__download_thread:
self.__download_thread.wait(3000)
self.__download_thread = None
index = self.DriverComboBox.currentIndex() index = self.DriverComboBox.currentIndex()
if 0 <= index < len(self.__driver_infos): if 0 <= index < len(self.__driver_infos):
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
@@ -458,13 +469,20 @@ class ALWebDriverDownloadDialog(QDialog):
if reply == QMessageBox.StandardButton.No: if reply == QMessageBox.StandardButton.No:
event.ignore() event.ignore()
return return
self.__download_thread.cancel() self.__download_thread.stop()
self.__download_thread.wait(5000)
if not self.__confirmed: if not self.__confirmed:
self.__selected_driver_info = None self.__selected_driver_info = None
event.accept() event.accept()
super().closeEvent(event) super().closeEvent(event)
def __onThreadFinished(
self
):
if self.__download_thread:
self.__download_thread.deleteLater()
self.__download_thread = None
def getSelectedDriverInfo( def getSelectedDriverInfo(
self self