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

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
B
Blog
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
B
Blog RSS Feed
WordPress大学
WordPress大学
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
O
OpenAI News
月光博客
月光博客
H
Hacker News: Front Page
S
Security Affairs
W
WeLiveSecurity
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
A
About on SuperTechFans

博客园 - 思无邪

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

由于异步完成事件不能确定,顺序也不能确定,如果把异步操作放到循环内,可能会产生意想不到的结果。

下面的程序本意是:循环items集合,为每个item赋值。

              foreach (BookRankItem item in items)
              {

//item只有部分属性有值

                  //Debug.WriteLine(item.Index);//输出0,1,2,3
                  EventHandler bookPageHander = (s, e) =>
                  {

                    //为item加载数据

                     BookPage bookPage = service.BookPage;

                      item.LoadFromPage(bookPage);
                      PagedItems.Add(item);
                       Debug.WriteLine(item.Index);//根据网络情况可能是3,3,3,3.只有最后一个
                      };
                   service.Loaded -= bookPageHander;
                  service.Loaded += bookPageHander;
                  service.Load(item.Uri);

由于赋值操作在异步内执行,执行完的顺序时间都是随机的,PagedItems内容可能是任意的Item。

如果外层循环到最后一个,异步才开始,会导致PagedItems加载的都是最后一项。