static void Main(string[] args) { //Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId); //Message message = new Message(); //Thread thread = new Thread(new ThreadStart(message.ShowMessage)); //thread.Start(); //Console.WriteLine("Do something ..........!"); //Console.WriteLine("Main thread working is complete!"); Message msg = new Message(); var del = new SumDel(msg.Sum); Console.WriteLine("begin"); IAsyncResult result = del.BeginInvoke(5, 6, null, null); Console.WriteLine("end"); var str = del.EndInvoke(result); Console.WriteLine("结果:"+str); Console.Read(); }
public static void RunDemo() { //Executing a Print function using delegate SimplePrintDel spd = Print; spd.Invoke(); //Executing a parameteric function using delegate PrintDelegate pd = Print; string msg = "This is another way of calling parametric delegate"; pd.Invoke(msg); //Executing a complex parametric function using delegate PrintGrades pgd = ShowGrades; pgd.Invoke(StudentGrades.Intermdiate); pgd.Invoke(StudentGrades.Advanced); pgd.Invoke(StudentGrades.Standard); //Multicasting Delegates pd += Print2; pd += Print3; pd.Invoke("Multi-Casting...."); //C# delegates ActionDel = Print; ActionDel2 = Print; ActionDel(); ActionDel2("Action Del 2"); //Retrun value delegate SumDel sum = Sum; var rv = sum.Invoke(10, 400); Console.WriteLine("Value : " + rv); FuncDel = Sum; rv = FuncDel.Invoke(103, 500); Console.WriteLine("Value : " + rv); Console.ReadKey(); }