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("主线程方法结束");
            }

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

  • 相关阅读:
    如果int x=20, y=5,则语句System.out.println(x+y +""+(x+y)+y); 的输出结果是()
    子父类存在同名成员时super的使用条件
    7mysql高级查询
    1udp编程
    6mysql外键
    4mysql数据表增删改查
    5mysql数据类型
    3mysql数据库操作
    2mysql基本使用
    1mysql安装
  • 原文地址:https://www.cnblogs.com/wangchuang/p/4913373.html
Copyright © 2011-2022 走看看