• Silverlight 中实现Service同步调用


    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;
    }
    测试例子
    public delegate void deleHeadler(object x, EventArgs e);
    02  
    03 public class RestService
    04 {
    05     public static void Query<T>(string s, deleHeadler del)
    06     {
    07         Console.WriteLine(s);
    08         for (int i = 0; i < 70000000; i++)
    09         {
    10  
    11         }
    12         del(null,null);
    13     }
    14 }
    15  
    16 public class Program
    17 {
    18     static void Main(string[] args)
    19     {
    20         int wellLoadedCount = 2;
    21         int loadCompletedCount = 0;
    22  
    23         RestService.Query<string>("1",
    24             (obj, argsOne) =>
    25             {
    26                 Interlocked.Increment(ref loadCompletedCount);
    27  
    28                 if (loadCompletedCount == wellLoadedCount)
    29                 {
    30                     UpdateUIAfterLoadData();
    31                 }
    32             });
    33  
    34  
    35         RestService.Query<string>("2",
    36             (obj, argsTwo) =>
    37             {
    38                 Interlocked.Increment(ref loadCompletedCount);
    39  
    40                 if (loadCompletedCount == wellLoadedCount)
    41                 {
    42                     UpdateUIAfterLoadData();
    43                 }
    44             });
    45         Console.ReadKey();
    46     }
    47          
    48     private static void UpdateUIAfterLoadData()
    49     {
    50         Console.WriteLine("全部加载完毕");
    51     }
    52 }
    53  
    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    struts2配置文件详解
    实体类和数据库映射--配置文件
    Hibernate常用配置文件详解
    本地计算机上的OracleOraDb11g_home1TNSListener服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。——Oracle监听器服务无法启动!
    关于远程访问Oracle数据库的设置(共享数据库)
    Log4J日志配置详解
    Java compiler level does not match the version of the installed Java project facet.问题
    Genymotion出现Unable to load VirtualBox engine问题--100%解决
    eclipse注解——作者,创建时间,版本
    web工程下的html中引用其他目录下的文件的path
  • 原文地址:https://www.cnblogs.com/Areas/p/2192166.html
Copyright © 2020-2023  润新知