community icon indicating copy to clipboard operation
community copied to clipboard

软件闲置时报错TargetParameterCountException

Open atBread opened this issue 1 month ago • 8 comments

检查清单 | Checklist

  • [x] 您在生产环境中发现了此问题,并稳定复现 | You have encountered this issue in a production environment and can reliably reproduce it.
  • [x] 您尝试了自行解决(包括但不限于重置配置文件,全新安装)但始终无法解决 | You have tried to resolve it yourself (including but not limited to resetting config files and doing a clean install), but could not fix it.
  • [x] 您可以在其它设备上稳定复现此问题 | You can reliably reproduce this issue on another device.
  • [x] 您确认此问题并非由实验性功能引起 | You confirm that this issue is not caused by an experimental feature.
  • [x] 您确认此问题严重影响了使用并且可明确定义为漏洞或缺陷 | You confirm that this issue seriously affects usage and can be clearly defined as a bug or defect.
  • [x] 您了解开发者无法及时回复和及时修复 | You understand that developers may not respond or fix issues promptly.
  • [x] 您确认、知晓无意义提交 Bug 类反馈的后果,并同意以下条款 | You confirm that you understand the consequences of submitting meaningless bug reports and agree to the following terms.
  • [x] 被认定为无意义的反馈将会被直接关闭 | Feedback deemed meaningless will be closed directly.
  • [x] 多次提交无意义的 Bug 类反馈将会被处以 7 天内不再接受您反馈的处罚 | Repeated meaningless bug reports will result in your feedback being suspended for 7 days.
  • [ ] 我已经仔细阅读过选项里的内容,并且知道这个选项不用勾选。 | I have carefully read the options and know that this option does not need to be checked.

软件版本 | App Version

v1.8.0.11

操作系统及版本 | OS & Version

Windows 10 19H2 教育版

问题描述 | Description

非常抱歉这个问题不是checklist里面提及的“其他设备稳定复现”的问题,但是我尝试重新安装了5次软件,该问题持续2个月,版本号从v1.8.0.3到现在的v1.8.0.11,均可以稳定发生该问题,所以尝试提交一下issue,希望开发者可以理解一下,辛苦帮我查看一下问题的原因。

在其他班级共计3台同款电脑,完全相同硬件、相同系统部署本软件;仅这一台设备出现该问题,其余设备运行稳定。

软件打开后,未发生任何触摸、鼠标交互操作,放置闲置一段时间,单击屏幕会自动弹出报错对话框。 点击关闭报错弹窗后,软件批注功能看起来完全正常,触摸批注等功能均可以使用,程序自行不会退出。

复现步骤 | Steps to Reproduce

闲置设备一段时间以后,以批注模式绘制一条线;或者在大量书写笔画在同一界面

期望结果 | Expected Behavior

修正报错问题

其他补充信息 | Additional Info

System.Reflection.TargetParameterCountException: Parameter count mismatch. at System.Reflection.MethodBaseInvoker.ThrowTargetParameterCountException() at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Image

上传有关文件 | Upload relevant files

No response

atBread avatar Oct 19 '26 15:10 atBread

是安装版吗 可以配合测试一下吗喵喵

AstrZero avatar Oct 20 '26 14:10 AstrZero

2. 是安装版吗 可以配合测试一下吗喵喵

是安装版,可以测啊怎么测

atBread avatar Oct 21 '26 14:10 atBread

我猜到原因了💩

PrefacedCorg avatar Oct 24 '26 17:10 PrefacedCorg

神了,查不出来,没有导致此原因的实际代码

CJKmkp avatar Oct 31 '26 13:10 CJKmkp

建议重装一下net吧

CJKmkp avatar Oct 31 '26 13:10 CJKmkp

已定位根因

崩溃点在 Ink Canvas/MainWindow.xaml.cs:1790(HandleFloatingBarRecovery()):

Dispatcher.Invoke(() =>
{
    isFloatingBarOutsideScreen = IsOutsideOfScreenHelper.IsOutsideOfScreen(ViewboxFloatingBar);
    isInPPTPresentationMode = IsInPPTPresentationMode;
}, DispatcherPriority.Normal, TimeSpan.FromSeconds(5));

WPF 的 Dispatcher 没有 Invoke(Action, DispatcherPriority, TimeSpan) 这个重载(Action 系只有 (Action)、(Action, priority)、(Action, priority, cancellationToken)、(Action, priority, cancellationToken, timeout))。所以这行被编译器绑定到了旧版重载:

public object Invoke(Delegate method, DispatcherPriority priority, params object[] args)

TimeSpan.FromSeconds(5) 并没有被当成“超时”,而是被当成 委托参数 包进 object[] { TimeSpan }。UI 线程执行时对 0 参 lambda 做 DynamicInvoke(TimeSpan),于是抛出:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
   at System.Reflection.MethodBaseInvoker.ThrowTargetParameterCountException()
   at System.Reflection.RuntimeMethodInfo.Invoke(...)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

与 issue 里贴出的堆栈逐帧一致。我用一个 net10.0-windows 最小程序按同样写法实测,得到完全相同的堆栈,以及同样的“调用方 try/catch 捕获不到”的现象。

为什么弹窗后程序一切正常

异常是在 UI 线程(Dispatcher 泵内)抛出的,不在 HandleFloatingBarRecovery 的后台线程上,所以 1804 行的 try/catch 捕获不到(日志里也不会出现“浮动工具栏恢复失败”)。异常最终进入 App_DispatcherUnhandledException(App.xaml.cs:1051):弹一次提示 + e.Handled = true → 应用继续运行,批注完全正常。每触发一次就再弹一次,所以会累积到 20 多个。

同时 lambda 体没有执行,isFloatingBarOutsideScreen 始终是 false,恢复逻辑等于没做。

触发条件(需要你确认)

只有两处会调用 HandleFloatingBarRecovery(),都在实验性开关后面:

  • SystemEventsOnDisplaySettingsChanged(MainWindow.xaml.cs:1763),需要 Settings.Advanced.IsEnableResolutionChangeDetection
  • MainWindow_OnDpiChanged(MainWindow.xaml.cs:1770),需要 Settings.Advanced.IsEnableDPIChangeDetection

这两个开关默认是 关 的,入口在 设置 → 实验性:「启用 ResolutionChangeDetection」「启用 DPIChangeDetection」。显示器休眠/唤醒、切投影、分辨率或 DPI 瞬时变化都会触发。

麻烦确认一下这台机器 安装目录\Configs\Settings.json 里 isEnableResolutionChangeDetection / isEnableDPIChangeDetection 是不是 true。如果是,根因就完全闭合了。

另外如果方便,请把 安装目录\Logs\Log_*.txt 里带 [Crash] 的那几段发上来(里面有完整的 ex.ToString())。完整堆栈里 TryCatchWhen 之后应该还能看到 MainWindow / HandleFloatingBarRecovery 的帧,可以直接印证。

修复

改成走带 CancellationToken 的重载:

Dispatcher.Invoke(() =>
{
    ...
}, DispatcherPriority.Normal, CancellationToken.None, TimeSpan.FromSeconds(5));

并把 lambda 内部包上 try/catch——外层的 try/catch 对 UI 线程异常无效。

我把主工程和依赖库(含 InkCanvasForClass.UI.WPF.Modern)都扫了一遍,这是唯一一处这种误用。修复会以 PR 提交。

JSR-Bot avatar Nov 11 '26 03:11 JSR-Bot

已定位到这个异常的具体来源,并提交了修复 PR:https://github.com/InkCanvasForClass/community/pull/785

根因

MainWindow.HandleFloatingBarRecovery() 里想「在 UI 线程上取值、最多等 5 秒」:

Dispatcher.Invoke(() => { ... }, DispatcherPriority.Normal, TimeSpan.FromSeconds(5));

但 Dispatcher 没有 Invoke(Action, DispatcherPriority, TimeSpan) 这个重载,只有 (Action)、(Action, priority)、(Action, priority, CancellationToken)、(Action, priority, CancellationToken, TimeSpan)。于是这行被编译期绑定到 legacy 的 Invoke(Delegate method, DispatcherPriority priority, params object[] args),TimeSpan 被当成委托参数塞进 args,UI 线程执行时 DynamicInvoke 参数个数不匹配 → TargetParameterCountException。

这个异常由 legacy DispatcherOperation 直接送到 Dispatcher.UnhandledException,不经过该方法的 try/catch,所以最后走到崩溃详情弹窗;因为 e.Handled = true,程序不会退出、批注照常,但会反复弹。

触发条件(解释了为什么只有部分设备复现)

两个入口,都由默认关闭的实验性开关控制(设置 → 实验性 → 「分辨率变化检测」/「DPI 变化检测」):

  • Settings.Advanced.IsEnableResolutionChangeDetection → SystemEventsOnDisplaySettingsChanged
  • Settings.Advanced.IsEnableDPIChangeDetection → MainWindow_OnDpiChanged

只要这两个开关开着,任何显示器配置/DPI 变化都会走这段代码。在开启的机器上,可以确认一下 Configs/Settings.json 里 isEnableResolutionChangeDetection / isEnableDPIChangeDetection 的值。

验证

本机(Win10 教育版)开启这两个开关后,向所有窗口广播 WM_DISPLAYCHANGE(等效真实分辨率变化,不用改分辨率):

  • 修复前:立即弹出「崩溃详情」,日志 UI线程未处理异常: System.Reflection.TargetParameterCountException。
  • 修复后:连续多次触发,无弹窗,日志无异常,程序正常响应;并临时加探针确认修复后的 Invoke 确实执行并回填了值(探针已移除)。

修复方式是显式传 CancellationToken.None 选择正确的重载,5 秒超时语义不变;超时现在会在调用方线程抛 TimeoutException 并被原有 catch 记录为警告,不会再变成未处理异常。

顺带说明:如果之后仍然弹出同类弹窗,麻烦把弹窗里「复制详情」的完整文本和 Logs 目录下的日志发一下,我们可以比对是否是另一处调用点。

JSR-Bot avatar Nov 11 '26 17:11 JSR-Bot

同一修复已按维护者要求同步到 et6 分支:#789(base et6,改动仅 Ink Canvas/MainWindow.xaml.cs 一处 Dispatcher.Invoke 重载选择,+4/-1)。

et6 上缺陷位于 MainWindow.HandleFloatingBarRecovery()(方法起始 L1718、问题调用 L1730),根因与 net10 相同:Dispatcher 没有 Invoke(Action, DispatcherPriority, TimeSpan) 重载,三参调用被绑定到 legacy Invoke(Delegate, priority, params object[]),TimeSpan 被当作委托参数,UI 线程 DynamicInvoke 时抛 TargetParameterCountException。

et6 侧的验证:未修复构建在广播 WM_DISPLAYCHANGE(两个实验性开关打开)后稳定弹出「崩溃详情」并记 TargetParameterCountException;修复构建在相同流程下连续触发 3 次无异常、进程正常响应。

JSR-Bot avatar Nov 12 '26 08:11 JSR-Bot