修复两个会导致软件无法使用的启动/校验缺陷

1) 提权重启无限循环:内置 Administrator 没有 UAC 分离令牌,自愈重启后
   仍是提权,于是每秒自我重启一次,窗口永远出不来。加 30 秒时间戳防抖,
   第二次直接留在提权状态运行并记录原因。
2) 自动校验静默卡死:ValidatePlaybackAsync 内部 await ConfigureAwait(false)
   之后仍直接写 WPF 控件,抛出的异常又被 fire-and-forget 吞掉,界面停在
   「正在校验 N 路回放…」。UI 更新统一切回 Dispatcher,并给入口加 try/catch
   把异常写进日志、显示到界面上。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-10-22 02:39:22 +08:00
parent c280e94b9a
commit fbcbd80acc
3 changed files with 132 additions and 46 deletions

View File

@ -1117,7 +1117,29 @@ public partial class MainWindow : Window
// 查询命中扫码记录 → **自动**校验回放(不用用户点按钮):
// 真的去录像机拉一下,确认这段时间有没有录像,再决定是否显示预览/下载。
if (_currentQuery is { Streams.Count: > 0 } query) _ = ValidatePlaybackAsync(query);
if (_currentQuery is { Streams.Count: > 0 } query)
{
// 不能直接 fire-and-forget:方法内部有 await ConfigureAwait(false),
// 异常进不了任何 try,界面会静默停在第一句提示上(现场表现为“卡住”)。
_ = RunValidatePlaybackAsync(query);
}
}
private async Task RunValidatePlaybackAsync(PackQuery query)
{
try
{
await ValidatePlaybackAsync(query).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// 新查询覆盖旧查询:正常
}
catch (Exception ex)
{
AppLog.Write($"[playback] 自动校验异常({query.Code}):{ex}");
ShowPlayback($"校验回放失败:{ex.Message}");
}
}
// ---------------- 回放:校验 / 下载 ----------------
@ -1180,10 +1202,33 @@ public partial class MainWindow : Window
private void ShowPlayback(string text)
{
// 校验流程里有 await ConfigureAwait(false),到这里可能已在线程池线程上,
// 直接写控件会抛“调用线程无法访问此对象”。统一切回 UI 线程。
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => ShowPlayback(text));
return;
}
PlaybackText.Text = text;
PlaybackPanel.Visibility = Visibility.Visible;
}
/// <summary>预览/下载按钮的显隐同样可能来自线程池线程,统一在这里切回 UI 线程。</summary>
private void SetRouteButtons(bool showPreview, bool showDownload)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => SetRouteButtons(showPreview, showDownload));
return;
}
PreviewButton.Visibility = showPreview ? Visibility.Visible : Visibility.Collapsed;
DownloadButton.Visibility = showDownload ? Visibility.Visible : Visibility.Collapsed;
PreviewButton.IsEnabled = showPreview;
DownloadButton.IsEnabled = showDownload;
}
/// <summary>
/// 自动校验回放:**真的去录像机拉一下**这段时间有没有录像
///
@ -1259,9 +1304,7 @@ public partial class MainWindow : Window
_validatedStreams = hasVideo ? streams : Array.Empty<PackStream>();
// 跨网只给下载:预览要实时拉流,云端中转成本高且体验差
PreviewButton.Visibility = Visibility.Collapsed;
DownloadButton.Visibility = hasVideo ? Visibility.Visible : Visibility.Collapsed;
DownloadButton.IsEnabled = hasVideo;
SetRouteButtons(showPreview: false, showDownload: hasVideo);
AppLog.Write($"[route] {query.Code} 云端校验:hasVideo={hasVideo}"
+ $"({cloud.ScannerSn},{cloud.Frames} 帧)→ 跨网只给下载");
@ -1276,8 +1319,7 @@ public partial class MainWindow : Window
if (cloud is { State: "no-plan" })
{
_validatedStreams = Array.Empty<PackStream>();
PreviewButton.Visibility = Visibility.Collapsed;
DownloadButton.Visibility = Visibility.Collapsed;
SetRouteButtons(showPreview: false, showDownload: false);
AppLog.Write($"[route] {query.Code} 云端判定无回放计划 → 不给按钮");
lines[0] = "这个运单算不出回放计划(扫码时该通道没绑定工作台)";
@ -1377,10 +1419,7 @@ public partial class MainWindow : Window
lines[0] = checkedStreams >= streams.Count
? $"校验完成:{valid.Count}/{streams.Count} 路有录像,可以预览或下载"
: $"校验完成:{valid[0].DisplayName} 有录像(其余 {streams.Count - checkedStreams} 路未检查),可以预览或下载";
PreviewButton.Visibility = Visibility.Visible;
DownloadButton.Visibility = Visibility.Visible;
PreviewButton.IsEnabled = true;
DownloadButton.IsEnabled = true;
SetRouteButtons(showPreview: true, showDownload: true);
}
else
{

View File

@ -74,6 +74,46 @@ public static class Program
app.Run();
}
/// <summary>
/// 自愈时间戳。内置 Administrator 这类没有 UAC 分离令牌的账户,重启出来仍然是提权,
/// 会无限自我重启(窗口永远出不来),所以 30 秒内只允许自愈一次。
/// </summary>
private static string RelaunchStampFile => System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"YuncangTrace", "elevated-relaunch.stamp");
/// <summary>刚自愈过就又提权启动 → 说明这台机器没有标准令牌可用,别再重启了。</summary>
private static bool HasJustRelaunched()
{
try
{
var file = RelaunchStampFile;
if (!System.IO.File.Exists(file)) return false;
var age = DateTime.UtcNow - System.IO.File.GetLastWriteTimeUtc(file);
System.IO.File.Delete(file);
return age < TimeSpan.FromSeconds(30);
}
catch
{
return false;
}
}
private static void MarkRelaunched()
{
try
{
var file = RelaunchStampFile;
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(file)!);
System.IO.File.WriteAllText(file, DateTime.UtcNow.ToString("O"));
}
catch
{
// 只是防抖标记,写不进去也不影响主流程
}
}
/// <summary>
/// 以标准权限重新启动自己。
///
@ -85,6 +125,12 @@ public static class Program
{
try
{
if (HasJustRelaunched())
{
AppLog.Write("[app] 重新以标准权限启动后仍是管理员(该账户没有 UAC 分离令牌),跳过自愈,继续以管理员运行");
return false;
}
var exe = Environment.ProcessPath;
if (string.IsNullOrEmpty(exe)) return false;
@ -96,6 +142,7 @@ public static class Program
WindowStyle = ProcessWindowStyle.Hidden,
});
MarkRelaunched();
AppLog.Write("[app] 检测到以管理员权限运行(旧版本升级继承),已用标准权限重新启动");
return true;
}

View File

@ -1,61 +1,61 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<!-- WPF 只能跑在 Windows 上;net8.0-windows 是当前 LTS -->
<!-- WPF 鍙兘璺戝湪 Windows 涓婏紱net8.0-windows 鏄綋鍓?LTS -->
<TargetFramework>net8.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<!-- 托盘图标用 WinForms 的 NotifyIcon(WPF 没有内置托盘) -->
<!-- 鎵樼洏鍥炬爣鐢?WinForms 鐨?NotifyIcon锛圵PF 娌℃湁鍐呯疆鎵樼洏锛?-->
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<!--
⚠️ 必须关掉隐式 using:WPF 与 WinForms 同时开启时,两边的隐式命名空间会让
MessageBox / Application / Clipboard / Point 等类型产生歧义(编译不过)。
各文件里显式 using 自己需要的命名空间。
鈿狅笍 蹇呴』鍏虫帀闅愬紡 using锛歐PF 涓?WinForms 鍚屾椂寮€鍚椂锛屼袱杈圭殑闅愬紡鍛藉悕绌洪棿浼氳
MessageBox / Application / Clipboard / Point 绛夌被鍨嬩骇鐢熸涔夛紙缂栬瘧涓嶈繃锛夈€?
鍚勬枃浠堕噷鏄惧紡 using 鑷繁闇€瑕佺殑鍛藉悕绌洪棿銆?
-->
<ImplicitUsings>disable</ImplicitUsings>
<ApplicationIcon>Assets\app.ico</ApplicationIcon>
<!-- 产物名/程序集名(安装包里 exe 就叫这个) -->
<!-- 浜х墿鍚?绋嬪簭闆嗗悕锛堝畨瑁呭寘閲?exe 灏卞彨杩欎釜锛?-->
<AssemblyName>YuncangTrace.LocalService</AssemblyName>
<RootNamespace>YuncangTrace.LocalService</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<!--
WFAC010:WinForms 分析器建议把高 DPI 从 manifest 挪到 ApplicationHighDpiMode。
但本程序是 WPF 宿主(走的是 WPF 的 App.Run,不会调 WinForms 的 ApplicationConfiguration),
WinForms 只用来放托盘图标 —— 那条建议在这里不生效,DPI 只能由 manifest 的 PerMonitorV2 决定。
WFAC010锛歐inForms 鍒嗘瀽鍣ㄥ缓璁妸楂?DPI 浠?manifest 鎸埌 ApplicationHighDpiMode銆?
浣嗘湰绋嬪簭鏄?WPF 瀹夸富锛堣蛋鐨勬槸 WPF 鐨?App.Run锛屼笉浼氳皟 WinForms 鐨?ApplicationConfiguration锛夛紝
WinForms 鍙敤鏉ユ斁鎵樼洏鍥炬爣 鈥斺€?閭f潯寤鸿鍦ㄨ繖閲屼笉鐢熸晥锛孌PI 鍙兘鐢?manifest 鐨?PerMonitorV2 鍐冲畾銆?
-->
<NoWarn>$(NoWarn);WFAC010</NoWarn>
<!--
环境配置(和前端 VITE_BACKEND 一个思路:由构建决定,不靠用户手填)
Debug :AppEnvironment=Development,默认后端 http://127.0.0.1:7300(= 前端 dev 代理的地址)
Release :AppEnvironment=Production ,默认后端由 -p:ServerBaseUrl=https://正式域名 覆盖
运行时覆盖优先级:命令行 server 参数 &gt; YUNCANG_SERVER 环境变量 &gt; 这里注入的默认值。
鐜閰嶇疆锛堝拰鍓嶇 VITE_BACKEND 涓€涓€濊矾锛氱敱鏋勫缓鍐冲畾锛屼笉闈犵敤鎴锋墜濉級
Debug 锛欰ppEnvironment=Development锛岄粯璁ゅ悗绔?http://127.0.0.1:7300锛? 鍓嶇 dev 浠g悊鐨勫湴鍧€锛?
Release 锛欰ppEnvironment=Production 锛岄粯璁ゅ悗绔敱 -p:ServerBaseUrl=https://姝e紡鍩熷悕 瑕嗙洊
杩愯鏃惰鐩栦紭鍏堢骇锛氬懡浠よ server 鍙傛暟 &gt; YUNCANG_SERVER 鐜鍙橀噺 &gt; 杩欓噷娉ㄥ叆鐨勯粯璁ゅ€笺€?
-->
<AppEnvironment Condition="'$(Configuration)' == 'Debug'">Development</AppEnvironment>
<AppEnvironment Condition="'$(Configuration)' == 'Release'">Production</AppEnvironment>
<ServerBaseUrl Condition="'$(Configuration)' == 'Debug' and '$(ServerBaseUrl)' == ''">http://127.0.0.1:7300</ServerBaseUrl>
<ServerBaseUrl Condition="'$(Configuration)' == 'Release' and '$(ServerBaseUrl)' == ''">https://packs.example.com</ServerBaseUrl>
<Version>1.0.105</Version>
<Product>云仓视频溯源软件</Product>
<Company>云仓</Company>
<AssemblyTitle>云仓视频溯源软件(本机服务)</AssemblyTitle>
<Version>1.0.106</Version>
<Product>浜戜粨瑙嗛婧簮杞欢</Product>
<Company>浜戜粨</Company>
<AssemblyTitle>浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級</AssemblyTitle>
<NeutralLanguage>zh-CN</NeutralLanguage>
<!--
同一个 exe 三种身份:桌面端 / Windows 服务(service 参数)/ 提权 helper(agent-control 参数)。
入口在 Program.cs;不写 StartupObject 的话 WPF 自动生成的 Main 会和它冲突(CS0017)。
鍚屼竴涓?exe 涓夌韬唤锛氭闈㈢ / Windows 鏈嶅姟锛坰ervice 鍙傛暟锛? 鎻愭潈 helper锛坅gent-control 鍙傛暟锛夈€?
鍏ュ彛鍦?Program.cs锛涗笉鍐?StartupObject 鐨勮瘽 WPF 鑷姩鐢熸垚鐨?Main 浼氬拰瀹冨啿绐侊紙CS0017锛夈€?
-->
<StartupObject>YuncangTrace.LocalService.Program</StartupObject>
</PropertyGroup>
<!-- 把环境写进程序集(Services/BuildInfo.cs 运行时读出来) -->
<!-- 鎶婄幆澧冨啓杩涚▼搴忛泦锛圫ervices/BuildInfo.cs 杩愯鏃惰鍑烘潵锛?-->
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>AppEnvironment</_Parameter1>
@ -68,28 +68,28 @@
</ItemGroup>
<!--
海康「设备网络 SDK(HCNetSDK Win64)」运行库,随程序输出到 native\HCNetSDK\。
运行时会按 环境变量 YUNCANG_HIKSDK_DIR → 程序目录\native\HCNetSDK → 程序目录 的顺序查找 HCNetSDK.dll。
这里用通配符,SDK 升级时只要替换目录内容即可,不用改工程。
娴峰悍銆岃澶囩綉缁?SDK锛圚CNetSDK Win64锛夈€嶈繍琛屽簱锛岄殢绋嬪簭杈撳嚭鍒?native\HCNetSDK\銆?
杩愯鏃朵細鎸?鐜鍙橀噺 YUNCANG_HIKSDK_DIR 鈫?绋嬪簭鐩綍\native\HCNetSDK 鈫?绋嬪簭鐩綍 鐨勯『搴忔煡鎵?HCNetSDK.dll銆?
杩欓噷鐢ㄩ€氶厤绗︼紝SDK 鍗囩骇鏃跺彧瑕佹浛鎹㈢洰褰曞唴瀹瑰嵆鍙紝涓嶇敤鏀瑰伐绋嬨€?
-->
<ItemGroup>
<Content Include="native\HCNetSDK\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<!--
⚠️ 必须排除单文件打包:默认会被塞进 exe 内部(自解压),
安装包只装 exe 时看似没问题,但 exe 体积暴涨且 native 目录不存在,
HCNetSDK 的依赖(HCCore.dll / HCNetSDKCom\*)解析会变得不可控。
显式让它作为**独立文件**输出到 publish\native\HCNetSDK\,由 MSI 一起安装。
鈿狅笍 蹇呴』鎺掗櫎鍗曟枃浠舵墦鍖咃細榛樿浼氳濉炶繘 exe 鍐呴儴锛堣嚜瑙e帇锛夛紝
瀹夎鍖呭彧瑁?exe 鏃剁湅浼兼病闂锛屼絾 exe 浣撶Н鏆存定涓?native 鐩綍涓嶅瓨鍦紝
HCNetSDK 鐨勪緷璧栵紙HCCore.dll / HCNetSDKCom\*锛夎В鏋愪細鍙樺緱涓嶅彲鎺с€?
鏄惧紡璁╁畠浣滀负**鐙珛鏂囦欢**杈撳嚭鍒?publish\native\HCNetSDK\锛岀敱 MSI 涓€璧峰畨瑁呫€?
-->
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<!--
ffmpeg(随程序输出到 native\ffmpeg\ffmpeg.exe):回放校验 / 录像合并 / 主码流转 MJPEG 都靠它。
之前没打进安装包,客户机必须自己装 ffmpeg,非服务端电脑就下不了合并后的成片。
同样要 ExcludeFromSingleFile —— 200MB+ 的单文件静态 exe 塞进自解压包没有意义。
运行时查找顺序:YUNCANG_FFMPEG → <exe目录>\native\ffmpeg\ffmpeg.exe → PATH
ffmpeg锛堥殢绋嬪簭杈撳嚭鍒?native\ffmpeg\ffmpeg.exe锛夛細鍥炴斁鏍¢獙 / 褰曞儚鍚堝苟 / 涓荤爜娴佽浆 MJPEG 閮介潬瀹冦€?
涔嬪墠娌℃墦杩涘畨瑁呭寘锛屽鎴锋満蹇呴』鑷繁瑁?ffmpeg锛岄潪鏈嶅姟绔數鑴戝氨涓嬩笉浜嗗悎骞跺悗鐨勬垚鐗囥€?
鍚屾牱瑕?ExcludeFromSingleFile 鈥斺€?200MB+ 鐨勫崟鏂囦欢闈欐€?exe 濉炶繘鑷В鍘嬪寘娌℃湁鎰忎箟銆?
杩愯鏃舵煡鎵鹃『搴忥細YUNCANG_FFMPEG 鈫?<exe鐩綍>\native\ffmpeg\ffmpeg.exe 鈫?PATH
-->
<Content Include="native\ffmpeg\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@ -98,10 +98,10 @@
</Content>
<!--
谷歌浏览器插件(仓库根目录 extension\):随程序输出到 native 同级的 extension\,
安装后位于 <安装目录>\extension,菜单「工具 → 复制谷歌插件地址」直接给出该 Windows 路径,
用户在 chrome://extensions 里「加载已解压的扩展程序」即可。
开发调试文件(test\ / tools\)不打进安装包。
璋锋瓕娴忚鍣ㄦ彃浠讹紙浠撳簱鏍圭洰褰?extension\锛夛細闅忕▼搴忚緭鍑哄埌 native 鍚岀骇鐨?extension\锛?
瀹夎鍚庝綅浜?<瀹夎鐩綍>\extension锛岃彍鍗曘€屽伐鍏?鈫?澶嶅埗璋锋瓕鎻掍欢鍦板潃銆嶇洿鎺ョ粰鍑鸿 Windows 璺緞锛?
鐢ㄦ埛鍦?chrome://extensions 閲屻€屽姞杞藉凡瑙e帇鐨勬墿灞曠▼搴忋€嶅嵆鍙€?
寮€鍙戣皟璇曟枃浠讹紙test\ / tools\锛変笉鎵撹繘瀹夎鍖呫€?
-->
<Content Include="..\extension\**\*" Exclude="..\extension\test\**;..\extension\tools\**">
<Link>extension\%(RecursiveDir)%(Filename)%(Extension)</Link>