zoukankan      html  css  js  c++  java
  • 委托回调的异步技术

      static void Main(string[] args)
            {
                //DateTime dt = DateTime.Now;
                //RemoteObject.MyObject app = new RemoteObject.MyObject();
                //Console.WriteLine(app.ALongTimeMethod(1, 2, 1000));
                //Method();
                //Console.WriteLine("用了" + ((TimeSpan)(DateTime.Now - dt)).TotalSeconds + "秒");
                //Console.ReadLine();
    
                DateTime dt = DateTime.Now;
                //RemoteObject.MyObject app=(RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
                RemoteObject.MyObject app = new RemoteObject.MyObject();
                md = new MyDelegate(app.ALongTimeMethod);
                AsyncCallback ac = new AsyncCallback(MyClient.CallBack);
                IAsyncResult Iar = md.BeginInvoke(1, 2, 1000, ac, null);
                Method();
                Console.WriteLine("用了" + ((TimeSpan)(DateTime.Now - dt)).TotalSeconds + "");
                Console.ReadLine();
            }
    
            private delegate int MyDelegate(int a, int b, int time);
            private static MyDelegate md;
            public static void CallBack(IAsyncResult Iar)
            {
                if (Iar.IsCompleted)
                {
                    Console.WriteLine("结果是" + md.EndInvoke(Iar));
                }
            }
            private static void Method()
            {
                Console.WriteLine("主线程方法开始");
                System.Threading.Thread.Sleep(3000);
                Console.WriteLine("主线程方法结束");
            }

    与普通代理异步没有多大的区别,只是多了一个参数,参数就是回调方法

  • 相关阅读:
    hdu1251统计难题(trie树)
    线段树
    poj2632Crashing Robots
    UVA10194 Football (aka Soccer)
    hdu1166敌兵布阵(线段树)
    【洛谷P3810】【模板】三维偏序(陌上花开)
    【洛谷P2480】古代猪文
    【洛谷P3449】PALPalindromes
    【洛谷P4777】扩展中国剩余定理(EXCRT)
    【洛谷P2421】荒岛野人
  • 原文地址:https://www.cnblogs.com/wangchuang/p/4913373.html
Copyright © 2011-2022 走看看