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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 牟向阳

Implementing SQL Server Row and Cell Level Security Daily English 你知道你的後照鏡調錯了嗎?原來裡面隱藏著視線死角! Silverlight Freezing & Crash Silverlight:获取ContentTemplate中的命名控件 silverlight 4常用的多线程技术 推荐几款Silverlight Tools【转载】 一个配置文件的Mapping Emit Vs CodeDom Custom DataContractSerializerOperationBehavior SQL Service查询分析 WPF&Silverlight精髓 支持定位当前页,自定义排序的分页SQL(拒绝动态SQL) WCF学习经验分享,如何更好地学习WCF? 后台管理界面收集 两个使用的Ajax Demo 自学面向对象 发几个有价值的.net源码 我是如何带领团队开发项目的
Silverlight 中实现Service同步调用
牟向阳 · 2011-09-14 · via 博客园 - 牟向阳

Silverlight中实现同步调用Service,核心是用到了MS的Interlocked.Increment.

Interlocked.Increment是做什么事情的?
如果有两个Thread ,分别记作threadA,threadB。

1:threadA将Value从存储空间取出,为0;

2:threadB将Value从存储空间取出,为0;

3:threadA将取出来的值和1作加法,并且将和放回Value的空间覆盖掉原值。加法结束,Value=1。

4:threadB将取出来的值和1作加法,并且将和放回Value的空间覆盖掉原值。加法结束,Value=1。

最后Value =1 ,而正确应该是2;这就是问题的所在,InterLockedIncrement 能够保证在一个线程访问变量时其它线程不能访问。

不废话了,直接上Demo了。

private BizModel bizModel = null;
public void LoadData()
{
    int loadCompletedCount = 0;
    int wellLoadedCount = 3;


    RestService.Query<BizEntity>("/Biz1/1000",(obj,args)=>
    {
	    Interlocked.Increment(ref loadCompletedCount);

     //business code
	    bizModel = args.Result.ToModel();

         if (loadCompletedCount == wellLoadedCount)
            {
               UpdateUIAfterLoadData();
            }

    }

    RestService.Query<BizEntity>("/Biz2/1000",(obj,args)=>
    {
	    Interlocked.Increment(ref loadCompletedCount);

     //business code
	    bizModel = args.Result.ToModel();

         if (loadCompletedCount == wellLoadedCount)
            {
               UpdateUIAfterLoadData();
            }

    }
    RestService.Query<BizEntity>("/Biz3/1000",(obj,args)=>
    {
	    Interlocked.Increment(ref loadCompletedCount);

     //business code
	    bizModel = args.Result.ToModel();

         if (loadCompletedCount == wellLoadedCount)
            {
               UpdateUIAfterLoadData();
            }

    }
}

private void UpdateUIAfterLoadData()
{
	//Refresh UI by bizModel;
}