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 = new RemoteObject.MyObject(); MyDelegate md = new MyDelegate(app.ALongTimeMethod); IAsyncResult Iar = md.BeginInvoke(1, 2, 1000, null, null); Method(); if (!Iar.IsCompleted) { Console.WriteLine("没有结束"); Iar.AsyncWaitHandle.WaitOne(); } else { Console.WriteLine("结果是" + md.EndInvoke(Iar)); } Console.WriteLine("用了" + ((TimeSpan)(DateTime.Now - dt)).TotalSeconds + "秒"); Console.ReadLine(); } private delegate int MyDelegate(int a, int b, int time); private static void Method() { Console.WriteLine("主线程方法开始"); System.Threading.Thread.Sleep(3000); Console.WriteLine("主线程方法结束"); }
时间: 2024-10-07 12:11:13