When PropertyChangedEventManager created/attached

To make a very long story short… This is in a large WPF project

I have a class “Patient” that implements INotifyPropertyChanged. When I dispose this class I am checking that the PropertyChangedEventHandler is null and if not run it to a ListenerDetector class that logs the listeners so we can track them down and clean the leaks. Patient does have some properties that are bound to WPF elements as well as other objects the use its PropertyChanged to monitor changes.

After disposing everything else, my log result is as follows:

Log: Patient still has the following listener(s) attached: - System.ComponentModel.PropertyChangedEventManager

Question: When is the PropertyChangedEventManager created?

  • When Patient is created
  • When WPF binds to Patient property
  • Some other point.

If Patient.PropertyChangedEventHandler is set to null and therefore disconnected from the PropertyChangedEventManager is there any way to recreate the Manager and have it listen to the Patient.PropertyChangedEventHandler again? Please don’t ask why this would happen, that is a point of tension here :-(


The PropertyChangedEventManager is created by WPF in order to support binding to any class that implements INotifyPropertyChanged. It'll be created and used as soon as you bind to any class that implements INotifyPropertyChanged.

That being said, its an implementation of the WeakEventManager pattern. While it's still showing a listener attached, realize that this listener is attached using the Weak Event Pattern. This will go away as soon as a full garbage collection occurs, as it's using weak references to hold the subscription. As such, this particular subscription shouldn't be the cause of a (long term) memory leak.

转载于:https://my.oschina.net/u/138995/blog/184254

#region namespace imports using System; using System.Collections; using System.Drawing; using System.IO; using System.Windows.Forms; using Cognex.VisionPro; using Cognex.VisionPro.ToolBlock; using Cognex.VisionPro3D; using Cognex.VisionPro.ImageProcessing; using Cognex.VisionPro.PMAlign; using Cognex.VisionPro.CalibFix; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion /// <summary> /// Called when the parent tool is run. /// Add code here to customize or replace the normal run behavior. /// </summary> /// <param name="message">Sets the Message in the tool's RunStatus.</param> /// <param name="result">Sets the Result in the tool's RunStatus</param> /// <returns>True if the tool should run normally, /// False if GroupRun customizes run behavior</returns> public override bool GroupRun(ref string message, ref CogToolResultConstants result) { // To let the execution stop in this script when a debugger is attached, uncomment the following lines. // #if DEBUG // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // #endif // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); return false; } #region When the Current Run Record is Created /// <summary> /// Called when the current record may have changed and is being reconstructed /// </summary> /// <param name="currentRecord"> /// The new currentRecord is available to be initialized or customized.</param> public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord) { } #endregion #region When the Last Run Record is Created /// <summary> /// Called when the last run record may have changed and is being reconstructed /// </summary> 在以上代码基础上写区分电池
03-28
<think>好的,我现在需要帮用户解决如何在现有的Cognex VisionPro工具块中添加区分电池的功能。用户已经有一个ToolBlock,现在想扩展功能来处理不同类型的电池。首先,我需要回顾VisionPro的基本流程,确保理解图像处理的步骤。 用户提到他们已经有了图像采集部分,可能已经能获取电池的图像。接下来,可能需要分析电池的特征,比如形状、尺寸、颜色或标签。不同的电池类型可能有不同的外观特征,比如AA和AAA电池的长度不同,或者锂电池可能有特定的标识。 首先,应该确定区分电池的具体特征。例如,如果是按尺寸分类,可能需要测量长度和直径。这时候可以使用VisionPro的Caliper工具或几何匹配工具来测量。如果是颜色或图案不同,可能需要使用颜色分析工具或OCR来识别标签。 然后,在ToolBlock中添加相应的工具。比如,添加Caliper工具来测量电池的长度,或者使用Color工具分析特定区域的颜色。每个工具需要正确配置参数,比如测量区域的位置、阈值等。可能还需要训练模式匹配工具,以便识别不同的电池类型。 接下来是分类逻辑的实现。在ToolBlock中,可以通过脚本工具(如CogScript)来编写判断逻辑,根据测量结果判断电池类型。例如,如果长度在某个范围内,则分类为AA电池。需要设置阈值参数,可能通过用户输入或配置文件来调整。 数据输出部分,需要将分类结果传递给下游系统,可能通过IO接口或数据库。同时,用户界面可能需要显示分类结果和置信度,方便监控和调试。 在集成和测试阶段,要确保各个工具协同工作,处理不同光照条件下的图像,调整参数以提高准确性。可能需要添加异常处理,比如无法分类的电池进行报警或人工检查。 还需要考虑扩展性,比如未来新增电池类型时,如何方便地添加新特征和逻辑。可能设计模块化的工具块,或者使用配置文件来管理特征参数。 最后,用户可能需要优化性能,确保处理速度满足产线要求。可能需要调整图像采集的分辨率,或优化算法参数以减少计算时间。 总结步骤:特征分析→工具添加→逻辑编写→输出集成→测试优化。同时注意系统的稳定性和扩展性。</think>在现有Cognex VisionPro工具块中添加电池分类功能,可通过以下步骤实现: $$分类准确度= \frac{正确识别样本数}{总样本数} \times 100\%$$ 1. **特征提取工具配置** - 使用`CogCaliperTool`测量电池尺寸参数(长度/直径) - 通过`CogColorSegmenterTool`分析外壳颜色特征 - 采用`CogPMAlignTool`匹配电池端盖特殊图案 2. **分类逻辑实现** ```python # 伪代码示例 if (length > 50.5±0.2mm) and (color == 蓝色): return "锂离子电池" elif (pattern_score > 0.8) and (diameter == 14.1mm): return "镍氢电池" ``` 3. **数据输出集成** - 在ToolBlock中添加`CogResultsAnalysis`工具 - 通过`CogSerial`或`CogEthernet`接口输出分类结果 - 使用`CogDatabase`工具存储检测记录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值