zoukankan      html  css  js  c++  java
  • Silverlight 中实现Service同步调用

    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;
    }
    
  • 相关阅读:
    在命令提示符中使用antlr
    Migrating to Rails 2.0.2
    从AJAX IN ACTION书中学用 RSS READER
    maple download url
    搜索
    发邀请在线RoR开发与部署环境www.heroku.com
    if can't use ruby in command line
    查询表中某字段有重复记录的个数
    WPF窗体自适应分辨率
    《思考,快与慢》
  • 原文地址:https://www.cnblogs.com/mxy1028/p/2175539.html
Copyright © 2011-2022 走看看