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); //委托的异步调用 IAsyncResult asyncResult = del.BeginInvoke(2, 5, null, null); //做某些其它的事情,模拟需要执行3秒钟 for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}:Client executed {1} second(s).", Thread.CurrentThread.Name, i); } //调用异步委托的结果 int rtn = del.EndInvoke(asyncResult); Console.WriteLine("Result: {0} ", rtn); //等待 Console.ReadKey(); }
static void Main(string[] args) { #region Program7 /* * Console.WriteLine("Client application started!\n"); * Thread.CurrentThread.Name = "Main Thread"; * Calculator cal = new Calculator(); * int result = cal.Add(2, 5); * Console.WriteLine("Result:{0}\n", result); * //做某些其他得事情,模拟需要执行3秒钟 * 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("\nPress any key to exit..."); * Console.ReadKey(); */ #endregion #region Program8 /* * Console.WriteLine("Client application started!\n"); * Thread.CurrentThread.Name = "Main Thread"; * Calculator cal = new Calculator(); * AddDelegate del = cal.Add; * IAsyncResult asyncResult = del.BeginInvoke(2, 5, null, null);// * //做某些其他得事情,模拟需要执行3秒钟 * for (int i = 1; i <= 3; i++) * { * Thread.Sleep(TimeSpan.FromSeconds(i)); * Console.WriteLine("{0}:Client executed{1} second(s).", Thread.CurrentThread.Name, i); * } * int rtn = del.EndInvoke(asyncResult); * Console.WriteLine("Result:{0}\n", rtn); * Console.WriteLine("\nPress any key to exit..."); * Console.ReadKey(); */ #endregion #region Program9 Console.WriteLine("Client application started!\n"); Thread.CurrentThread.Name = "Main Thread"; Calculator cal = new Calculator(); AddDelegate del = cal.Add; string data = "Any data you want to pass."; AsyncCallback callBack = new AsyncCallback(OnAddComplete); del.BeginInvoke(2, 5, callBack, data); //做某些其他得事情,模拟需要执行3秒钟 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("\nPress any key to exit..."); Console.ReadKey(); #endregion }
static void Main(string[] args) { int result; AddDelegate add = new AddDelegate(Add); Console.WriteLine("[Main] Invoking the asynchronous Add method"); add.BeginInvoke(6, 42, out result, new AsyncCallback(AnnounceSum), add); Thread.Sleep(1000); Console.ReadLine(); }
static void Main(string[] args) { int l = 3, r = 4; AddDelegate add = new AddDelegate(Add); Data d = new Data(); IAsyncResult ar = add.BeginInvoke(l, r, new AsyncCallback(Callback), d); d.mre.WaitOne(); // write result in main thread // alternatively, could write result in Callback Console.WriteLine("Result is {0} ", d.data); Console.ReadKey(); }
static void Main(string[] args) { int l = 3, r = 4; AddDelegate add = new AddDelegate(Add); IAsyncResult ar = add.BeginInvoke(l, r, null, null); //while (!ar.IsCompleted) Thread.Sleep(10); // or ar.AsyncWaitHandle.WaitOne(); // write result in main thread // if async delegate causes an exception it will become visible when EndInvoke is called try { Console.WriteLine("Result is {0} ", add.EndInvoke(ar)); } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { Console.WriteLine("Client application started!\n"); Thread.CurrentThread.Name = "Main Thread"; Calculator cal = new Calculator(); AddDelegate del = new AddDelegate(cal.Add); string data = "Any data you want to pass."; AsyncCallback callback = new AsyncCallback(OnAddComplete); del.BeginInvoke(2, 5, callback, data); for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(1)); Console.WriteLine("{0}:Add executed {1} second(s).", Thread.CurrentThread.Name, i); } Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); }
public static void Main1234() { Console.WriteLine("Client application started!\n"); Thread.CurrentThread.Name = "Main Thread"; Calculator cal = new Calculator(); AddDelegate del = new AddDelegate(cal.Add); string data = "Any data you want to pass."; AsyncCallback callback = new AsyncCallback(OnComplete); del.BeginInvoke(2, 5, callback, data); //异步调用方法 // 做某些其它的事情,模拟需要执行3 秒钟 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("\nPress any key to exit..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Client application started!\n"); Thread.CurrentThread.Name = "Main Thread"; Calculator cal = new Calculator(); AddDelegate del = new AddDelegate(cal.Add); IAsyncResult asyncResult = del.BeginInvoke(2, 5, null, null);// 异步调用方法 // 做某些其他的事情,模拟需要执行3秒钟 for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}: Client executed {1} second(s).", Thread.CurrentThread.Name, i); } int rtn = del.EndInvoke(asyncResult); Console.WriteLine("Result: {0}\n", rtn); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("客户端应用程序开始执行"); Thread.CurrentThread.Name = "主线程"; Calculator cal = new Calculator(); AddDelegate del = new AddDelegate(cal.Add); IAsyncResult asr = del.BeginInvoke(2, 3, null, null); //模拟3秒钟 for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}:客户端线程停留{1}", Thread.CurrentThread.Name, i); } int result = del.EndInvoke(asr); Console.WriteLine("结果:{0}", result); Console.WriteLine("结束"); Console.ReadLine(); }
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); IAsyncResult asyncResult = del.BeginInvoke(1, 2, null, null); for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}:Client excuted {1} seconds.", Thread.CurrentThread.Name, i); } int result = del.EndInvoke(asyncResult); Console.WriteLine("Result:" + result); Console.WriteLine("Press any key exist"); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Client application start!\n"); Thread.CurrentThread.Name = "Main thread"; Calculator cal = new Calculator(); AddDelegate addDelegate = new AddDelegate(cal.Add); IAsyncResult asyncResult = addDelegate.BeginInvoke(2, 5, null, null);//这里需要保留IAsyncResult,以便调用EndInvoke的时候进行传递 //BeginInvoke(int x, int y, AsyncCallback callback, object @object) for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}: Client executed {1} second(s).", Thread.CurrentThread.Name, i); } int rtn = addDelegate.EndInvoke(asyncResult);//调用EndInvoke,得到返回值 Console.WriteLine("Result: {0}", rtn); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("枚举:"); EnumGreetPeople("枚举", Language.Chinese); EnumGreetPeople("Enum", Language.English); Console.WriteLine(); Console.WriteLine("委托:"); GreetingDelegate delegate1, delegate2; delegate1 = ChineseGreeting; delegate2 = EnglishGreeting; GreetPeopple("委托", delegate1); GreetPeopple("delegate", delegate2); Console.WriteLine(); Console.WriteLine("委托绑定一:"); GreetingDelegate delegate3; delegate3 = ChineseGreeting; delegate3 += EnglishGreeting; GreetPeopple("binding", delegate3); Console.WriteLine(); Console.WriteLine("委托绑定二:"); delegate3("bundingtwo"); Console.WriteLine(); Console.WriteLine("委托解绑:"); delegate3 -= EnglishGreeting; delegate3("unbunding"); Console.WriteLine(); Console.WriteLine("新类:"); GreetingManage gm = new GreetingManage(); gm.GreetPeople("gm类", ChineseGreeting); gm.GreetPeople("classgm", EnglishGreeting); Console.WriteLine(); Console.WriteLine("委托新类:"); GreetingManage gm2 = new GreetingManage(); GreetingDelegate delegate4; delegate4 = ChineseGreeting; delegate4 += EnglishGreeting; gm2.GreetPeople("gmclasstwo", delegate4); Console.WriteLine(); Console.WriteLine("新类定义的委托:"); GreetingManage gm3 = new GreetingManage(); gm3.gmGreetingDelegate = ChineseGreeting; gm3.gmGreetingDelegate += EnglishGreeting; gm3.gmGreetingDelegate("gmdelegate"); Console.WriteLine(); Console.WriteLine("模拟烧水过程,Observer设计模式:"); Console.WriteLine(); Heater heater = new Heater(); Alarm alarm = new Alarm(); Display display = new Display(); heater.Boiled += alarm.MakeAlert; //注册方法 heater.Boiled += (new Alarm()).MakeAlert; //给匿名对象注册方法 heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册 heater.Boiled += Display.ShowMsg; //注册静态方法 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 Console.WriteLine(); Console.WriteLine("不使用异步调用的通常情况:"); Console.WriteLine("Client application start"); Thread.CurrentThread.Name = "Main Thread"; Calculator cal = new Calculator(); int result = cal.Add(6, 8); Console.WriteLine("Result:{0}", result); //做其他事情,模拟需要执行3秒钟 for (int i = 1; i <= 3; i++) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine("{0}:Client excute {1} second(s).", Thread.CurrentThread.Name, i); } Console.WriteLine(); Console.WriteLine("使用异步调用:"); Console.WriteLine("Client application started!\n"); //Thread.CurrentThread.Name = "Main Thread"; AddDelegate del = new AddDelegate(cal.Add); string data = "And data you want to pass"; AsyncCallback callback = new AsyncCallback(Calculator.OnAddComplete); IAsyncResult asyncResult = del.BeginInvoke(5, 6, callback, data); //异步调用方法 del.EndInvoke(asyncResult); // 做某些其它的事情,模拟需要执行3 秒钟 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.ReadLine(); Console.ReadKey(); }