Skip to content

Commit

Permalink
feat: cancel scan
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodCoder666 committed Apr 2, 2024
1 parent 7326479 commit 234f7f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def __test_ips(self, after_scan=True):
if after_scan:
self.progressBar.setValue(0)
self.progressBar.setMaximum(len(ips))
self.ui.btnWait_Scan.setText('扫描')
else:
self.__init_progessBar(len(ips))
thread = SpeedtestThread(self, ips, self.__add_result, self.__found_unavailable,
Expand All @@ -318,11 +319,16 @@ def __got_scan_result(self, ip):

def __scan_finished(self):
self.__set_buttons_enabled(True)
self.ui.btnWait_Scan.setText('扫描')
self.__remove_progessBar()
self.ui.statusbar.showMessage('扫描完成')

@Slot()
def on_btnWait_Scan_clicked(self):
if self.ui.btnWait_Scan.text() == '取消':
self.ui.btnWait_Scan.setEnabled(False)
self.sthread.cancel()
return
dlg = dlgScan(self)
if dlg.exec() == QDialog.Accepted:
max_ips = dlg.ui.spinBox_MaxIP.value()
Expand All @@ -345,6 +351,10 @@ def on_btnWait_Scan_clicked(self):
self.logLabel.setText(f'开始扫描,共 {total_addrs} 个 IP...')
thread.start()

self.sthread = thread
self.ui.btnWait_Scan.setEnabled(True)
self.ui.btnWait_Scan.setText('取消')

def dragEnterEvent(self, event):
event.accept()

Expand Down
6 changes: 5 additions & 1 deletion threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ def __add_block(self):
self.currentIndex += 1
return True

def cancel(self):
self.running = False

def run(self):
self.running = True
self.pool = QThreadPool()
self.pool.setMaxThreadCount(self.num_workers)
self.counter = 0
while self.counter < self.max_ips and self.__add_block():
while self.running and self.counter < self.max_ips and self.__add_block():
self.pool.waitForDone()
self.progressUpdate.emit(self.num_added)
self.pool.waitForDone()
Expand Down

0 comments on commit 234f7f3

Please sign in to comment.