Skip to content

修复串口列表刷新偶发失败后永久停摆 - #234

Open
LuaRobot wants to merge 1 commit into
chenxuuu:masterfrom
LuaRobot:fix/port-list-refresh
Open

LuaRobot wants to merge 1 commit into
chenxuuu:masterfrom
LuaRobot:fix/port-list-refresh

Conversation

@LuaRobot

@LuaRobot LuaRobot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Fixes #210

顺着「开机时间长 + 软件一直开着,串口列表就空掉」这条线看,refreshPortList() 里有三个问题,第一个能直接解释「列表没了」:

  1. skipSearch 一旦置 true 就再也不会复位。WMI 查询连续失败 3 次(searchCount >= 3)会把它设为 true,之后每次刷新的 while (!skipSearch) 直接不进循环,只剩 SerialPort.GetPortNames() 那段兜底;兜底项本来就没有设备名,看起来就是「串口列表不显示」。现在每次刷新开头复位它。
  2. WMI 对象没释放。ManagementObjectSearcher 和每次 searcher.Get() 出来的 ManagementObject 都没 Dispose,挂一天反复刷新会一直涨句柄,久了枚举变慢/失败,正好是「时间越长越容易出现」的形态。改成 using。
  3. \0 截断的兜底判断写成了 IndexOf("\0") > 0,字符串以 \0 开头时不生效(应该是 >= 0),顺手改掉。

说明:本机是 Linux,这个 WPF 老式 csproj 编不了,没法实跑验证;如果有可复现的环境,麻烦看一下失败 3 次后点刷新是不是能重新拿到带设备名的列表。

- skipSearch 一旦因为 WMI 连续失败 3 次被置 true,之后永远不再复位,
  之后所有刷新都只剩 SerialPort.GetPortNames() 的兜底项,表现为列表变空/
  没有设备名;现在每次刷新开头复位它
- ManagementObjectSearcher/ManagementObject 用完没释放,长时间挂着软件
  反复刷新会漏句柄,改为 using 释放
- 微软库 \0 截断的兜底判断是 IndexOf > 0,字符串以 \0 开头时不生效,改为 >= 0

Fixes chenxuuu#210

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

电脑开机时间长了+工具使用时间长了就会出现无法显示串口列表

1 participant