C# 委托简单例子
public delegate double Delegate_Prod(int a,int b); class Class1 { static double fn_Prodvalues(int val1,int val2) { return val1*val2; } static void Main(string[] args) { //Creating the Delegate Instance Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues); Console.Write("Please Enter Values"); int v1 = Int32.Parse(Console.ReadLine()); int v2 = Int32.Parse(Console.ReadLine()); //use a delegate for processing double res = delObj(v1,v2); Console.WriteLine ("Result :"+res); Console.ReadLine(); } }
namespace ConsoleApplication { //定义代理 delegate String Mydelegate(); class temp { static void Main(String[] args) { //实例化代理 Mydelegate Md = new Mydelegate(AsyncMethod); //实例一个回调代理 AsyncCallback callback = new AsyncCallback(callbackMethod); //开始执行异步方法 Md.BeginInvoke(callback, Md); Console.ReadLine(); } //异步调用的方法 static String AsyncMethod() { Console.WriteLine("异步方法正执行"); String str = "异步调用已结束"; return str; } //回调方法 static void callbackMethod(IAsyncResult Ias) { Mydelegate Md = (Mydelegate)Ias.AsyncState; String str = Md.EndInvoke(Ias); Console.WriteLine(str); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Runtime.Remoting.Messaging; namespace ConsoleTest { public delegate int AddHandler(int a,int b); public class 加法类 { public static int Add(int a, int b) { Console.WriteLine("开始计算:" + a + "+" + b); Thread.Sleep(3000); //模拟该方法运行三秒 Console.WriteLine("计算完成!"); return a + b; } } public class 同步调用 { static void Main() { Console.WriteLine("===== 同步调用 SyncInvokeTest ====="); AddHandler handler = new AddHandler(加法类.Add); int result = handler.Invoke(1, 2); Console.WriteLine("继续做别的事情。。。"); Console.WriteLine(result); Console.ReadKey(); } } public class 异步调用 { static void Main() { Console.WriteLine("===== 异步调用 AsyncInvokeTest ====="); AddHandler handler = new AddHandler(加法类.Add); //IAsyncResult: 异步操作接口(interface) //BeginInvoke: 委托(delegate)的一个异步方法的开始 IAsyncResult result = handler.BeginInvoke(1, 2, null, null); Console.WriteLine("继续做别的事情。。。"); //异步操作返回 Console.WriteLine(handler.EndInvoke(result)); Console.ReadKey(); } } public class 异步回调 { static void Main() { Console.WriteLine("===== 异步回调 AsyncInvokeTest ====="); AddHandler handler = new AddHandler(加法类.Add); //异步操作接口(注意BeginInvoke方法的不同!) IAsyncResult result = handler.BeginInvoke(1,2,new AsyncCallback(回调函数),"AsycState:OK"); Console.WriteLine("继续做别的事情。。。"); Console.ReadKey(); } static void 回调函数(IAsyncResult result) {
//result 是“加法类.Add()方法”的返回值 //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。 AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate; Console.WriteLine(handler.EndInvoke(result)); Console.WriteLine(result.AsyncState); } } }