线程还是会时不时暴毙,还需要优化

This commit is contained in:
2022-01-06 00:50:11 +08:00
parent ac1d49e48c
commit 4e122d5ad4
6 changed files with 56 additions and 37 deletions
+5 -3
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.1, 2022-01-05T20:45:49. -->
<!-- Written by QtCreator 6.0.1, 2022-01-06T00:49:24. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -240,12 +240,14 @@
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/wuwen/Desktop/tcp_ip_scan/ip_sacaner/ip_sacaner.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/wuwen/Desktop/tcp_ip_scan/ip_sacaner/ip_sacaner.pro</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/wuwen/Desktop/tcp_ip_scan/build-ip_sacaner-Desktop_Qt_6_2_2_MinGW_64_bit-Debug</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
+12 -12
View File
@@ -30,7 +30,6 @@ void trytry::run()
{
//qDebug() << ipstr<<":"<< ipint;
QTcpSocket m_socket;
m_socket.connectToHost(ipstr, ipint, QTcpSocket::ReadWrite);
if (m_socket.waitForConnected(timeout))
@@ -39,22 +38,20 @@ void trytry::run()
*outputbusy=1;
QString temp = QString("%1:%2").arg(ipstr).arg(ipint);
output_list->append(temp);
//msleep(1);
*outputbusy=0;
}
m_socket.disconnectFromHost();
m_socket.disconnect();
//msleep(timeout);//test
//qDebug()<<*nt_bar<<"/"<<*t_bar;
while(*barbusy);
*barbusy=1;
*now_thread_num-=1;
*nt_bar+=1;
//((*nt_bar)/(*t_bar))*100
//the_bar->setValue(((0.0+*nt_bar)/(*t_bar))*100);
//qDebug()<<((0.0+*nt_bar)/(0.0+*t_bar))*100<<"%";
//the_bar->setValue(10);
qDebug()<<*nt_bar<<"/"<<*t_bar;
//str.asprintf("停止 %.3f",((0.0+*nt_bar)/(0.0+*t_bar))*100);
bt->setText(QString("%1%").arg(((0.0+*nt_bar)/(0.0+*t_bar))*100));
*barbusy=0;
}
dispatch::dispatch()
@@ -80,7 +77,7 @@ void dispatch::tray(const QString& ipstr, quint32 ipint)
try_telnet->ipint = ipint;
try_telnet->timeout = timeout;
try_telnet->output_list = output_list;
try_telnet->bt=bt;
try_telnet->now_thread_num = &now_thread_num;
@@ -88,8 +85,9 @@ void dispatch::tray(const QString& ipstr, quint32 ipint)
try_telnet->nt_bar=&nt_bar;
try_telnet->outputbusy=&outputbusy;
try_telnet->barbusy=&barbusy;
try_telnet->the_bar=the_bar;
//try_telnet->moveToThread(main_thread);
try_telnet->start();
@@ -101,6 +99,8 @@ void dispatch::tray(const QString& ipstr, quint32 ipint)
void dispatch::run()
{
//qDebug()<< ui->IP_list->toPlainText();
//the_bar->setValue(20);
+7 -5
View File
@@ -2,12 +2,13 @@
#define SCAN_RUN_H
#include <QThread>
#include "widget.h"
#include "ui_widget.h"
#include <QTextEdit>
#include <QProgressBar>
#include <QDebug>
#include <QTcpSocket> //socket
#include <QLabel>
@@ -16,16 +17,16 @@ class trytry : public QThread
public:
quint16 *outputbusy;
quint16 *barbusy;
quint16 timeout;
QString ipstr;
quint32 ipint;
int *now_thread_num;
QTextEdit* output_list;
QLabel *bt;
quint64 *t_bar;
quint64 *nt_bar;
QProgressBar* the_bar;
void run();
@@ -44,11 +45,12 @@ public:
quint64 t_bar=0;
quint64 nt_bar=0;
quint16 outputbusy=0;
quint16 barbusy=0;
//QThread* main_thread;
QTextEdit* ip_list;
QTextEdit* port_list;
QTextEdit* output_list;
QProgressBar* the_bar;
QLabel *bt;
trytry* try_telnet;
void tray(const QString& ipstr, quint32 ipint);
+7 -4
View File
@@ -42,20 +42,22 @@ Widget::Widget(QWidget *parent)
ui->stard_scan->setText("停止");
dispatch* dispatch_thread;
//dispatch* dispatch_thread;
dispatch_thread = new dispatch;
dispatch_thread->ip_list = ui->IP_list;
dispatch_thread->port_list = ui->port_list;
dispatch_thread->output_list = ui->outputlist;
dispatch_thread->bt=ui->type_bar;
dispatch_thread->set_thread_num = ui->threads->value();
dispatch_thread->now_thread_num = 0;
dispatch_thread->timeout = ui->timeout->value();
dispatch_thread->the_bar=ui->type_bar;
//dispatch_thread->main_thread = main_thread;
//dispatch_thread->moveToThread(main_thread);
dispatch_thread->start();
connect(dispatch_thread, &dispatch::dispatch_finish, [=]()
connect(dispatch_thread, &dispatch::finished, [=]()
{
ui->IP_list->setReadOnly(false);
ui->port_list->setReadOnly(false);
@@ -69,7 +71,8 @@ Widget::Widget(QWidget *parent)
});
}else
{
//dispatch_thread->exit();
qDebug() << "tray_exit";
dispatch_thread->terminate();
}
+1
View File
@@ -21,6 +21,7 @@ public:
Widget(QWidget *parent = nullptr);
~Widget();
dispatch *dispatch_thread;
void auto_edit();
+24 -13
View File
@@ -28,9 +28,9 @@
<widget class="QTextEdit" name="port_list">
<property name="geometry">
<rect>
<x>500</x>
<x>490</x>
<y>100</y>
<width>91</width>
<width>101</width>
<height>111</height>
</rect>
</property>
@@ -56,10 +56,21 @@
<rect>
<x>420</x>
<y>220</y>
<width>171</width>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="text">
<string>开始扫描</string>
</property>
@@ -152,13 +163,13 @@ domain.com</string>
<property name="geometry">
<rect>
<x>10</x>
<y>300</y>
<y>270</y>
<width>581</width>
<height>191</height>
<height>221</height>
</rect>
</property>
<property name="readOnly">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="label_2">
@@ -193,17 +204,17 @@ domain.com</string>
<number>100</number>
</property>
</widget>
<widget class="QProgressBar" name="type_bar">
<widget class="QLabel" name="type_bar">
<property name="geometry">
<rect>
<x>10</x>
<y>270</y>
<width>581</width>
<height>23</height>
<x>500</x>
<y>240</y>
<width>53</width>
<height>16</height>
</rect>
</property>
<property name="value">
<number>0</number>
<property name="text">
<string>0%</string>
</property>
</widget>
</widget>