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("主线程方法结束"); }
与普通代理异步没有多大的区别,只是多了一个参数,参数就是回调方法