惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
F
Fortinet All Blogs
U
Unit 42
F
Full Disclosure
雷峰网
雷峰网
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
罗磊的独立博客
D
DataBreaches.Net
C
Check Point Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
O
OpenAI News
C
CXSECURITY Database RSS Feed - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
P
Proofpoint News Feed
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
Google Online Security Blog
Google Online Security Blog
H
Help Net Security
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
M
MIT News - Artificial intelligence
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
MyScale Blog
MyScale Blog
腾讯CDC

博客园 - 思无邪

Silverlight内存泄露(八)样式 Silverlight内存泄露(七)Command Silverlight内存泄露(六)MEF等Ioc框架引起内存泄露-ExportLifetimeContext Silverlight内存泄露(五)MEF等Ioc框架引起内存泄露-PartCreationPolicy Silverlight内存泄露(四)解决内存泄露 Silverlight内存泄露(二)检测内存泄露 从火狐放弃依据特性更新版本说起 Silverlight内存泄露(二)解决内存泄露之Dispose误用 Silverlight内存泄露(一)序 Silverlight IReader阅读器第二版 silverlight阅读器——面向领域的浏览器(二)——Silverlight阅读器架构 silverlight阅读器——起源面向领域的浏览器——概念 silverlight异步陷阱(二)循环 IReader Silverlight电子阅读器介绍开源项目 silverlight在线阅读器(二):为silverlight增加gb2312编码 silverlight在线阅读器(一):介绍 新建立一个类似于CuteEditor的项目,希望有人参加。发布一个测试版本的dll 从控件开发的角度看几个editor控件,Freetextbox,radtoolbar,abouteditor,cuteeditor 想做一个关于word解析和HtmlEditor的项目,希望有人加入 cuteEditor6.0的破解方式与Cuteeditor6.0的脚本调试
silverlight 异步陷阱(一)不能Remove事件处理程序
思无邪 · 2011-02-16 · via 博客园 - 思无邪

Silverlight中许多操作都要去异步完成,但有时候会碰到多个异步操作需要按照一定顺序完成,事件是把异步操作转换成同步操作的一种方法。

lambda是操作事件的简洁方式,事件、lambda、同步组合在一起,就会产生事件处理程序不能移除的状况。看下面的代码:


代码

public void LoadChapter(string uri)
      {
          
//两个异步操作需要协调

          EventHandler handler 
= (s, e) =>
          {
              
if (this.PagedContentListView == null)
              {
                  BookContentService contentService 
= new BookContentService();
                  EventHandler loadContentHandler 
= (ss, args) =>
                  {
                     
//必须在外层执行完后执行

                  };
                  contentService.Loaded 
-= loadContentHandler;
                  contentService.Loaded 
+= loadContentHandler;
                  contentService.Load(UriGenratorFaced.GeneratBookContentUri(uri));
              }

              Chapter 

= service.BookChapter;
                        };
          service.Loaded 
-= handler;
          service.Loaded 
+= handler;       } 

两个事件处理程序嵌套,保证内层的loadContentHandler在外层handler执行后执行,使两个异步操作同步。

看代码逻辑应该能够正常运行,但是每执行一次LoadChapter方法,handler事件处理程序就会被注册一次,导致handler的执行次数加1。

问题的原因是由于内层嵌套的loadContentHandler ,导致service.Loaded -= handler,不能移除handler。

解决方法:问题找到了,解决方法可以有两种。

1、使事件处理程序只能注册一次

比如不使用lambda,在类的构造函数中执行事件处理程序。

使用lambda但是增加标记值,使事件只注册一次。

自定义事件注册方式等。

2、每次移除所有事件处理程序

参见:http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control