委托异步执行
示例一
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateTest { public delegate string MyDelegate(object data); class Program { static void Main(string[] args) { MyDelegate mydelegate = new MyDelegate(TestMethod); IAsyncResult result = mydelegate.BeginInvoke("Thread Param", TestCallback, "Callback Param"); //异步执行完成 string resultstr = mydelegate.EndInvoke(result); Console.WriteLine("resultstr=" + resultstr); Console.Read(); } //线程函数 public static string TestMethod(object data) { string datastr = data as string; return datastr; } //异步回调函数 public static void TestCallback(IAsyncResult data) { Console.WriteLine(data.AsyncState); } } }
运行测试