zoukankan      html  css  js  c++  java
  • 委托和异步方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting.Messaging;
    using System.Text;
    using System.Threading;
    
    namespace 委托和异步方法
    {
        public delegate int AddDelegate(int x ,int y);
        public class Program
        {
            //执行回调方法的线程并非客户端线程Main Thread
            public static void onAddCom(IAsyncResult asyncResult) {
                AsyncResult result = (AsyncResult)asyncResult;
                AddDelegate del= (AddDelegate)result.AsyncDelegate;
                string data = (string)asyncResult.AsyncState;
                int rtn =del.EndInvoke(asyncResult);
                Console.WriteLine("{0}: Result, {1}; Data: {2} ",
                Thread.CurrentThread.Name, rtn, data);
            }
            static void Main(string[] args)
            {
                Console.WriteLine("Client application started! ");
                Thread.CurrentThread.Name = "Main Thread";
    
                Calculator cal = new Calculator();
    
                AddDelegate del = new AddDelegate(cal.add);
    
    
                //同步调用
                int result= (int)del.DynamicInvoke(new object[] { 1, 2 });
    
                Console.WriteLine("DynamicInvoke Result: {0}", result);
    
                AsyncCallback callback = new AsyncCallback(onAddCom);
    
                //异步调用
                IAsyncResult asyncResult = del.BeginInvoke(1, 2, callback, "This is a data string");
    
    
    
                for (int i = 1; i <= 3; i++)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(i));
                    Console.WriteLine("{0}: Client executed {1} second(s).",
                        Thread.CurrentThread.Name, i);
                }
    
                Console.WriteLine(" Press any key to exit...");
                Console.ReadKey();
    
            }
        }
    }
    

      

  • 相关阅读:
    寻找我编程道路的明灯
    Torque2D MIT 学习笔记(7) TAML的使用
    Torque2D MIT 学习笔记(4) 脚本语法(2)
    C++输入/输出流
    设计模式之命令模式
    设计模式之策略模式
    Torque2D MIT 学习笔记(11) 资源管理(3)
    C++文件处理
    Torque2D MIT 学习笔记(2) 目录结构
    设计模式之观察者模式
  • 原文地址:https://www.cnblogs.com/Jeely/p/11003619.html
Copyright © 2011-2022 走看看