Weak Event Patterns
https://msdn.microsoft.com/en-US/library/aa970850(v=vs.100).aspx
In applications, it is possible that handlers that are attached to event sources will not be destroyed in coordination with the listener object that attached the handler to the source.
This situation can lead to memory leaks.
Windows Presentation Foundation (WPF) introduces a design pattern that can be used to address this issue, by providing a dedicated manager class for particular events and implementing an interface on listeners for that event.
This design pattern is known as the weak event pattern.
Why Implement the Weak Event Pattern?
Listening for events can lead to memory leaks. The typical technique for listening to an event is to use the language-specific syntax that attaches a handler to an event on a source.
For example, in C#, that syntax is: source.SomeEvent += new SomeEventHandler(MyEventHandler).
This technique creates a strong reference from the event source to the event listener.
Ordinarily, attaching an event handler for a listener causes the listener to have an object lifetime that is influenced by the object lifetime of the source (unless the event handler is explicitly removed).
But in certain circumstances, you might want the object lifetime of the listener to be controlled by other factors, such as whether it currently belongs to the visual tree of the application, and not by the lifetime of the source.
Whenever the source object lifetime extends beyond the object lifetime of the listener, the normal event pattern leads to a memory leak: the listener is kept alive longer than intended.
The weak event pattern is designed to solve this memory leak problem.
The weak event pattern can be used whenever a listener needs to register for an event, but the listener does not explicitly know when to unregister.
The weak event pattern can also be used whenever the object lifetime of the source exceeds the useful object lifetime of the listener. (In this case, useful is determined by you.)
The weak event pattern allows the listener to register for and receive the event without affecting the object lifetime characteristics of the listener in any way.
In effect, the implied reference from the source does not determine whether the listener is eligible for garbage collection.
The reference is a weak reference, thus the naming of the weak event pattern and the related APIs.
The listener can be garbage collected or otherwise destroyed, and the source can continue without retaining noncollectible handler references to a now destroyed object.
Who Should Implement the Weak Event Pattern?
Implementing the weak event pattern is interesting primarily for control authors. As a control author, you are largely responsible for the behavior and containment of your control and the impact it has on applications in which it is inserted. This includes the control object lifetime behavior, in particular the handling of the described memory leak problem.
Certain scenarios inherently lend themselves to the application of the weak event pattern. One such scenario is data binding. In data binding, it is common for the source object to be completely independent of the listener object, which is a target of a binding. Many aspects of WPF data binding already have the weak event pattern applied in how the events are implemented.
How to Implement the Weak Event Pattern?
Implementing the weak event pattern consists of the following three aspects:
Derive a manager from the WeakEventManager class.
Implement the IWeakEventListener interface on any class that wants to register listeners for the weak event without generating a strong reference to the source.
When registering listeners, do not use the conventional add and remove accessors of the event where you want the listener to use the pattern. Instead, use the AddListener and RemoveListener implementations in the dedicated WeakEventManager for that event.
WeakEventManager
To implement the weak event pattern, you typically create a manager class with a 1:1 relationship to the event.
For example, if you have an event named Spin, you would create a SpinEventManager class that is the dedicated weak event manager for the event.
If the event exists in more than one class, behaves generally the same in each class, and shares the same event data type, the same manager can be used for each event.
When you derive from the WeakEventManager class, you override two virtual methods and expose several other members whose names are not specifically governed by a virtual template, but should exist nonetheless.
The overrides are used to initiate or terminate event delivery mode by the WPF infrastructure.
The other members provide functionality so that your own IWeakEventListener implementations can use theWeakEventManager to attach listeners to the event.
For more information about deriving from WeakEventManager, see the "Notes to Inheritors" section in the WeakEventManager reference topic.
IWeakEventListener
The IWeakEventListener interface has a single interface method named ReceiveWeakEvent. The ReceiveWeakEvent implementation must be a centralized implementation that directs any event reference that exists on that class to the appropriate WeakEventManager.
For more information about implementing the IWeakEventListener interface, see the "Notes to Implementers" section in the ReceiveWeakEventmethod reference topic.
Weak Event Patterns的更多相关文章
- WPF: 深入理解 Weak Event 模型
在之前写的一篇文章(XAML: 自定义控件中事件处理的最佳实践)中,我们曾提到了在 .NET 中如果事件没有反注册,将会引起内存泄露.这主要是因为当事件源会对事件监听者产生一个强引用,导致事件监听者无 ...
- The .NET weak event pattern in C#
Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...
- c#弱事件(weak event)
传统事件publisher和listener是直接相连的,这样会对垃圾回收带来一些问题,例如listener已经不引用任何对象但它仍然被publisher引用 垃圾回收器就不能回收listener所占 ...
- Data Binding和INotifyPropertyChanged是如何协调工作的?
前言 WPF的一大基础就是Data Binding.在基于MVVM架构的基础上,只有通过实现INotifyPropertyChanged接口的ViewModel才能够用于Data Binding. 要 ...
- Windows Phone 8内存控制研究 之 LonglistSelector使用陷阱
最近工作中常常被问到如何降低WP内存使用,便再一次开始研究内存问题,首先发现了LonglistSelector使用的一个常见问题: 概述 若将Longlistselector 控件的ItemsSour ...
- ventBroker简单实现
C#编程实践—EventBroker简单实现 前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.n ...
- C#编程实践—EventBroker简单实现
前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...
- 每天翻译一点点: WPF Application Framework (WAF)
ps:http://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&referringTitle=Document ...
- 三种观察者模式的C#实现
系列主题:基于消息的软件架构模型演变 说起观察者模式,估计在园子里能搜出一堆来.所以写这篇博客的目的有两点: 观察者模式是写松耦合代码的必备模式,重要性不言而喻,抛开代码层面,许多组件都采用了Publ ...
随机推荐
- ios客户端base64上传图片到java服务器遇到的问题
由于base64位包含了“+”和“\”两个特殊符号,导致ios编码后上传图片到服务器,服务器解码以后的值会不一致,导致图片损坏. 解决办法:重写Base64类,用“(”和“)”替换“+”和“\”两个特 ...
- maven增量编译
最近由于不清楚maven(2.2.x)增量编译的机制,导致应用出现了一个当时觉得非常诡异的一个问题.先描述一下问题. 背景是应用A有一个公用的base包,版本为1.6.6-SNAPSHOT,应 ...
- php function 定义时函数名前加&符号的意义
看了很多帖子,但是都不能理解,又去看了很多资料,终于名白了.记下备忘. 问题:php在声明函数时,函数名前面的&符号有什么用? 一直想不通.很多帖子说类似于变量的$a=&$b,但是$b ...
- Have Fun with Numbers (大数)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- AJAX 基础知识
1.AJAX 是一种用于创建快速动态网页的技术.而是一种用于创建更好更快以及交互性更强的 Web 应用程序的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不 ...
- java se doc
J2SE 5.0 Performance White Paper http://www.oracle.com/technetwork/java/5-136747.html Java Tuning Wh ...
- 1014: [JSOI2008]火星人prefix - BZOJ
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- aJax提交——服务端不能用request存储数据,session存数据客户端可以接收到
aJax提交与普通提交是两种迥异的提交方式,这两种提交方式决定了客户端与服务端交互时存储.传输数据的方式也不同. aJax提交,客户端的请求数据存储在data中,服务端用request.getPara ...
- firefly 环境配置所需工具
原地址:http://www.9miao.com/question-15-59032.html http://down.9miao.com/attachment/forum/201405/19/120 ...
- 荣誉,还是苦逼?| 也议全栈工程师和DevOps
引言 全栈工程师(本文称「全栈」开发者)和 DevOps 无疑是近期最火的词汇,无论是国外还是国内.而且火爆程度远超于想象. 全栈和 DevOps,究竟是我们的新职业方向,还是仅仅创业公司老板的心头所 ...