@@ -762,8 +762,7 @@ void CTrafficMonitorDlg::ApplySettings(COptionsDlg& optionsDlg)
762762 {
763763 KillTimer (MONITOR_TIMER);
764764 SetTimer (MONITOR_TIMER, theApp.m_general_data .monitor_time_span , NULL );
765- // m_timer.KillTimer();
766- // m_timer.CreateTimer((DWORD_PTR)this, theApp.m_general_data.monitor_time_span, MonitorThreadCallback);
765+ m_thread_monitor_counter = 1 ; // 重置线程中的监控计数器
767766 }
768767
769768 // 设置获取CPU利用率的方式
@@ -1112,8 +1111,7 @@ BOOL CTrafficMonitorDlg::OnInitDialog()
11121111 SetTimer (MAIN_TIMER, 1000 , NULL );
11131112
11141113 SetTimer (MONITOR_TIMER, theApp.m_general_data .monitor_time_span , NULL );
1115- // m_timer.CreateTimer((DWORD_PTR)this, theApp.m_general_data.monitor_time_span, MonitorThreadCallback);
1116-
1114+ AfxBeginThread (MonitorThreadCallback, (LPVOID)this );
11171115
11181116 // 初始化窗口位置
11191117 SetItemPosition ();
@@ -1164,70 +1162,66 @@ static int GetMonitorTimerCount(int second)
11641162}
11651163
11661164
1167- UINT CTrafficMonitorDlg::MonitorThreadCallback (LPVOID dwUser )
1165+ void CTrafficMonitorDlg::DoMonitorAcquisition ( )
11681166{
1169- CTrafficMonitorDlg* pThis = (CTrafficMonitorDlg*)dwUser;
1170- CFlagLocker flag_locker (pThis->m_is_monitor_thread_runing );
1171-
11721167 // 获取网络连接速度
11731168 int rtn{};
1174- auto getLfTable = [&]()
1175- {
1169+ auto getLfTable = [&]() {
11761170 __try
11771171 {
1178- rtn = GetIfTable (pThis-> m_pIfTable , &pThis-> m_dwSize , FALSE );
1172+ rtn = GetIfTable (m_pIfTable, &m_dwSize, FALSE );
11791173 }
11801174 __except (EXCEPTION_EXECUTE_HANDLER)
11811175 {
1182- free (pThis-> m_pIfTable );
1183- pThis-> m_dwSize = sizeof (MIB_IFTABLE);
1184- pThis-> m_pIfTable = (MIB_IFTABLE*)malloc (pThis-> m_dwSize );
1185- rtn = GetIfTable (pThis-> m_pIfTable , &pThis-> m_dwSize , FALSE );
1176+ free (m_pIfTable);
1177+ m_dwSize = sizeof (MIB_IFTABLE);
1178+ m_pIfTable = (MIB_IFTABLE*)malloc (m_dwSize);
1179+ rtn = GetIfTable (m_pIfTable, &m_dwSize, FALSE );
11861180 if (rtn == ERROR_INSUFFICIENT_BUFFER) // 如果函数返回值为ERROR_INSUFFICIENT_BUFFER,说明m_pIfTable的大小不够
11871181 {
1188- free (pThis-> m_pIfTable );
1189- pThis-> m_pIfTable = (MIB_IFTABLE*)malloc (pThis-> m_dwSize ); // 用新的大小重新开辟一块内存
1182+ free (m_pIfTable);
1183+ m_pIfTable = (MIB_IFTABLE*)malloc (m_dwSize); // 用新的大小重新开辟一块内存
11901184 }
1191- GetIfTable (pThis-> m_pIfTable , &pThis-> m_dwSize , FALSE );
1185+ GetIfTable (m_pIfTable, &m_dwSize, FALSE );
11921186 }
11931187 };
11941188
11951189 getLfTable ();
11961190
11971191 if (!theApp.m_cfg_data .m_select_all ) // 获取当前选中连接的网速
11981192 {
1199- auto table = pThis-> GetConnectIfTable (pThis-> m_connection_selected );
1200- pThis-> m_in_bytes = table.dwInOctets ;
1201- pThis-> m_out_bytes = table.dwOutOctets ;
1193+ auto table = GetConnectIfTable (m_connection_selected);
1194+ m_in_bytes = table.dwInOctets ;
1195+ m_out_bytes = table.dwOutOctets ;
12021196 }
12031197 else // 获取全部连接的网速
12041198 {
1205- pThis-> m_in_bytes = 0 ;
1206- pThis-> m_out_bytes = 0 ;
1207- for (size_t i{}; i < pThis-> m_connections .size (); i++)
1199+ m_in_bytes = 0 ;
1200+ m_out_bytes = 0 ;
1201+ for (size_t i{}; i < m_connections.size (); i++)
12081202 {
1209- auto table = pThis-> GetConnectIfTable (i);
1203+ auto table = GetConnectIfTable (i);
12101204 // if (i > 0 && m_pIfTable->table[m_connections[i].index].dwInOctets == m_pIfTable->table[m_connections[i - 1].index].dwInOctets
12111205 // && m_pIfTable->table[m_connections[i].index].dwOutOctets == m_pIfTable->table[m_connections[i - 1].index].dwOutOctets)
12121206 // continue; //连接列表中可能会有相同的连接,统计所有连接的网速时,忽略掉已发送和已接收字节数完全相同的连接
1213- pThis-> m_in_bytes += table.dwInOctets ;
1214- pThis-> m_out_bytes += table.dwOutOctets ;
1207+ m_in_bytes += table.dwInOctets ;
1208+ m_out_bytes += table.dwOutOctets ;
12151209 }
12161210 }
12171211
12181212 unsigned __int64 cur_in_speed{}, cur_out_speed{}; // 本次监控时间间隔内的上传和下载速度
12191213
12201214 // 如果发送和接收的字节数为0或上次发送和接收的字节数为0或当前连接已改变时,网速无效
1221- if ((pThis-> m_in_bytes == 0 && pThis-> m_out_bytes == 0 ) || (pThis-> m_last_in_bytes == 0 && pThis-> m_last_out_bytes == 0 ) || pThis-> m_connection_change_flag
1222- || pThis-> m_last_in_bytes > pThis-> m_in_bytes || pThis-> m_last_out_bytes > pThis-> m_out_bytes )
1215+ if ((m_in_bytes == 0 && m_out_bytes == 0 ) || (m_last_in_bytes == 0 && m_last_out_bytes == 0 ) || m_connection_change_flag
1216+ || m_last_in_bytes > m_in_bytes || m_last_out_bytes > m_out_bytes)
12231217 {
12241218 cur_in_speed = 0 ;
12251219 cur_out_speed = 0 ;
12261220 }
12271221 else
12281222 {
1229- cur_in_speed = pThis-> m_in_bytes - pThis-> m_last_in_bytes ;
1230- cur_out_speed = pThis-> m_out_bytes - pThis-> m_last_out_bytes ;
1223+ cur_in_speed = m_in_bytes - m_last_in_bytes;
1224+ cur_out_speed = m_out_bytes - m_last_out_bytes;
12311225 }
12321226 // //如果大于1GB/s,说明可能产生了异常,网速无效
12331227 // if (cur_in_speed > 1073741824)
@@ -1247,66 +1241,66 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
12471241 theApp.m_in_speed = static_cast <unsigned __int64>(cur_in_speed * 1000 / time_span);
12481242 theApp.m_out_speed = static_cast <unsigned __int64>(cur_out_speed * 1000 / time_span);
12491243
1250- pThis-> m_connection_change_flag = false ; // 清除连接发生变化的标志
1244+ m_connection_change_flag = false ; // 清除连接发生变化的标志
12511245
1252- pThis-> m_last_in_bytes = pThis-> m_in_bytes ;
1253- pThis-> m_last_out_bytes = pThis-> m_out_bytes ;
1246+ m_last_in_bytes = m_in_bytes;
1247+ m_last_out_bytes = m_out_bytes;
12541248
12551249 // 处于自动选择状态时,如果连续30秒没有网速,则可能自动选择的网络不对,此时执行一次自动选择
12561250 if (theApp.m_cfg_data .m_auto_select )
12571251 {
12581252 if (cur_in_speed == 0 && cur_out_speed == 0 )
1259- pThis-> m_zero_speed_cnt ++;
1253+ m_zero_speed_cnt++;
12601254 else
1261- pThis-> m_zero_speed_cnt = 0 ;
1262- if (pThis-> m_zero_speed_cnt >= GetMonitorTimerCount (30 ))
1255+ m_zero_speed_cnt = 0 ;
1256+ if (m_zero_speed_cnt >= GetMonitorTimerCount (30 ))
12631257 {
1264- pThis-> AutoSelect ();
1265- pThis-> m_zero_speed_cnt = 0 ;
1258+ AutoSelect ();
1259+ m_zero_speed_cnt = 0 ;
12661260 }
12671261 }
12681262
12691263 // 检测当前日期是否改变,如果已改变,就向历史流量列表插入一个新的日期
12701264 SYSTEMTIME current_time;
12711265 GetLocalTime (¤t_time);
1272- if (pThis-> m_history_traffic .GetTraffics ()[0 ].day != current_time.wDay )
1266+ if (m_history_traffic.GetTraffics ()[0 ].day != current_time.wDay )
12731267 {
12741268 HistoryTraffic traffic;
12751269 traffic.year = current_time.wYear ;
12761270 traffic.month = current_time.wMonth ;
12771271 traffic.day = current_time.wDay ;
12781272 traffic.mixed = false ;
1279- pThis-> m_history_traffic .GetTraffics ().push_front (traffic);
1273+ m_history_traffic.GetTraffics ().push_front (traffic);
12801274 theApp.m_today_up_traffic = 0 ;
12811275 theApp.m_today_down_traffic = 0 ;
12821276 }
12831277
12841278 // 统计今天已使用的流量
12851279 theApp.m_today_up_traffic += cur_out_speed;
12861280 theApp.m_today_down_traffic += cur_in_speed;
1287- pThis-> m_history_traffic .GetTraffics ()[0 ].up_kBytes = theApp.m_today_up_traffic / 1024u ;
1288- pThis-> m_history_traffic .GetTraffics ()[0 ].down_kBytes = theApp.m_today_down_traffic / 1024u ;
1281+ m_history_traffic.GetTraffics ()[0 ].up_kBytes = theApp.m_today_up_traffic / 1024u ;
1282+ m_history_traffic.GetTraffics ()[0 ].down_kBytes = theApp.m_today_down_traffic / 1024u ;
12891283 // 每隔30秒保存一次流量历史记录
1290- if (pThis-> m_monitor_time_cnt % GetMonitorTimerCount (30 ) == GetMonitorTimerCount (30 ) - 1 )
1284+ if (m_monitor_time_cnt % GetMonitorTimerCount (30 ) == GetMonitorTimerCount (30 ) - 1 )
12911285 {
12921286 static unsigned __int64 last_today_kbytes;
1293- if (pThis-> m_history_traffic .GetTraffics ()[0 ].kBytes () - last_today_kbytes >= 100u ) // 只有当流量变化超过100KB时才保存历史流量记录,防止磁盘写入过于频繁
1287+ if (m_history_traffic.GetTraffics ()[0 ].kBytes () - last_today_kbytes >= 100u ) // 只有当流量变化超过100KB时才保存历史流量记录,防止磁盘写入过于频繁
12941288 {
1295- pThis-> SaveHistoryTraffic ();
1296- last_today_kbytes = pThis-> m_history_traffic .GetTraffics ()[0 ].kBytes ();
1289+ SaveHistoryTraffic ();
1290+ last_today_kbytes = m_history_traffic.GetTraffics ()[0 ].kBytes ();
12971291 }
12981292 }
12991293
13001294 if (rtn == ERROR_INSUFFICIENT_BUFFER)
13011295 {
1302- pThis-> IniConnection ();
1296+ IniConnection ();
13031297 CString info;
13041298 info.LoadString (IDS_INSUFFICIENT_BUFFER);
1305- info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (pThis-> m_restart_cnt ));
1299+ info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (m_restart_cnt));
13061300 CCommon::WriteLog (info, theApp.m_log_path .c_str ());
13071301 }
13081302
1309- if (pThis-> m_monitor_time_cnt % GetMonitorTimerCount (3 ) == GetMonitorTimerCount (3 ) - 1 )
1303+ if (m_monitor_time_cnt % GetMonitorTimerCount (3 ) == GetMonitorTimerCount (3 ) - 1 )
13101304 {
13111305 // 重新获取当前连接数量
13121306 static DWORD last_interface_num = -1 ;
@@ -1320,15 +1314,15 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
13201314 info.LoadString (IDS_CONNECTION_NUM_CHANGED);
13211315 info.Replace (_T (" <%before%>" ), CCommon::IntToString (last_interface_num));
13221316 info.Replace (_T (" <%after%>" ), CCommon::IntToString (interface_num));
1323- info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (pThis-> m_restart_cnt + 1 ));
1317+ info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (m_restart_cnt + 1 ));
13241318 CCommon::WriteLog (info, theApp.m_log_path .c_str ());
13251319 }
1326- pThis-> IniConnection ();
1320+ IniConnection ();
13271321 last_interface_num = interface_num;
13281322 }
13291323
13301324 string descr;
1331- descr = (const char *)pThis-> GetConnectIfTable (pThis-> m_connection_selected ).bDescr ;
1325+ descr = (const char *)GetConnectIfTable (m_connection_selected).bDescr ;
13321326 if (descr != theApp.m_cfg_data .m_connection_name )
13331327 {
13341328 // 写入额外的调试信息
@@ -1343,10 +1337,10 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
13431337 CCommon::WriteLog (log_str, (theApp.m_config_dir + L" .\\ connections.log" ).c_str ());
13441338 }
13451339
1346- pThis-> IniConnection ();
1340+ IniConnection ();
13471341 CString info;
13481342 info.LoadString (IDS_CONNECTION_NOT_MATCH);
1349- info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (pThis-> m_restart_cnt ));
1343+ info.Replace (_T (" <%cnt%>" ), CCommon::IntToString (m_restart_cnt));
13501344 CCommon::WriteLog (info, theApp.m_log_path .c_str ());
13511345 }
13521346 }
@@ -1362,14 +1356,14 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
13621356 // 获取CPU使用率
13631357 if (lite_version || theApp.m_general_data .cpu_usage_acquire_method != GeneralSettingData::CA_HARDWARE_MONITOR || !theApp.m_general_data .IsHardwareEnable (HI_CPU))
13641358 {
1365- theApp.m_cpu_usage = pThis-> m_cpu_usage_helper .GetCPUUsage ();
1359+ theApp.m_cpu_usage = m_cpu_usage_helper.GetCPUUsage ();
13661360 cpu_usage_acquired = true ;
13671361 }
13681362
13691363 // 获取CPU频率
13701364 // if (lite_version || is_arm64ec || !theApp.m_general_data.IsHardwareEnable(HI_CPU))
13711365 // {
1372- if (pThis-> m_cpu_freq_helper .GetCpuFreq (theApp.m_cpu_freq ))
1366+ if (m_cpu_freq_helper.GetCpuFreq (theApp.m_cpu_freq ))
13731367 cpu_freq_acquired = true ;
13741368 // }
13751369
@@ -1383,21 +1377,21 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
13831377
13841378#ifndef WITHOUT_TEMPERATURE
13851379 // 获取温度
1386- if (pThis-> IsTemperatureNeeded () && theApp.m_pMonitor != nullptr )
1380+ if (IsTemperatureNeeded () && theApp.m_pMonitor != nullptr )
13871381 {
13881382 CSingleLock sync (&theApp.m_minitor_lib_critical , TRUE );
13891383
13901384 auto getHardwareInfo = []()
1391- {
1392- __try
1393- {
1394- theApp.m_pMonitor ->GetHardwareInfo ();
1395- }
1396- __except (EXCEPTION_EXECUTE_HANDLER)
13971385 {
1398- AfxMessageBox (IDS_HARDWARE_INFO_ACQUIRE_FAILED_ERROR, MB_ICONERROR | MB_OK);
1399- }
1400- };
1386+ __try
1387+ {
1388+ theApp.m_pMonitor ->GetHardwareInfo ();
1389+ }
1390+ __except (EXCEPTION_EXECUTE_HANDLER)
1391+ {
1392+ AfxMessageBox (IDS_HARDWARE_INFO_ACQUIRE_FAILED_ERROR, MB_ICONERROR | MB_OK);
1393+ }
1394+ };
14011395
14021396 getHardwareInfo ();
14031397 auto monitor_error_message{ OpenHardwareMonitorApi::GetErrorMessage () };
@@ -1500,22 +1494,39 @@ UINT CTrafficMonitorDlg::MonitorThreadCallback(LPVOID dwUser)
15001494 }
15011495 }
15021496
1503- pThis-> m_monitor_time_cnt ++;
1497+ m_monitor_time_cnt++;
15041498
15051499 // 发送监控信息更新消息
1506- pThis->SendMessage (WM_MONITOR_INFO_UPDATED);
1500+ SendMessage (WM_MONITOR_INFO_UPDATED);
1501+ }
1502+
1503+ UINT CTrafficMonitorDlg::MonitorThreadCallback (LPVOID dwUser)
1504+ {
1505+ CTrafficMonitorDlg* pThis = (CTrafficMonitorDlg*)dwUser;
1506+ while (true )
1507+ {
1508+ // 监控计数器大于0时获取一次监控数据
1509+ if (pThis->m_thread_monitor_counter > 0 )
1510+ {
1511+ pThis->DoMonitorAcquisition ();
1512+ // 每获取一次监控数据,计数减1
1513+ pThis->m_thread_monitor_counter --;
1514+ }
1515+ else
1516+ {
1517+ Sleep (10 );
1518+ }
1519+ }
15071520
15081521 return 0 ;
15091522}
15101523
15111524void CTrafficMonitorDlg::OnTimer (UINT_PTR nIDEvent)
15121525{
1513-
15141526 // TODO: 在此添加消息处理程序代码和/或调用默认值
15151527 if (nIDEvent == MONITOR_TIMER)
15161528 {
1517- if (!m_is_monitor_thread_runing) // 确保线程已退出
1518- AfxBeginThread (MonitorThreadCallback, (LPVOID)this );
1529+ m_thread_monitor_counter++;
15191530 }
15201531
15211532 if (nIDEvent == MAIN_TIMER)
0 commit comments