今天第一次用live Writer写代码,先发一个! 异步代码。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Asyncalback { public delegate string deleDoing(string argsInput); class AsyncalCall { private static deleDoing deleDoingObj = new deleDoing(SaySomeThingsNeedLitterOfTime); // 三部曲,定义委托,二 定义方法执行(主要是begininvoke,和endinvoke),和,三是返回结果存放位置 private static string SomeThingsNeedLitterOfTimeResult = string.Empty; //返回结果的变量 public static void Main() { Console.WriteLine("开始工作。。。"); string inWorkinArges = "参数"; //费事的工作传入参数 deleDoingObj.BeginInvoke(inWorkinArges, new AsyncCallback(TaskFinished), null); //执行 doAnotherThinges(); Console.ReadLine(); } /// <summary> /// 模拟其它执行过程 /// </summary> private static void doAnotherThinges() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); Console.WriteLine("其它程序"+System.DateTime.Now.ToString()); Thread.Sleep(100); Console.WriteLine("其它程序"+System.DateTime.Now.ToString()); } } /// <summary> /// 返回的结果费时的结果 执行的情况 /// </summary> /// <param name="result"></param> public static void TaskFinished(IAsyncResult result) { Console.WriteLine("执行结果回调一次"+System.DateTime.Now.ToString()); SomeThingsNeedLitterOfTimeResult = deleDoingObj.EndInvoke(result); Console.WriteLine("执行结果回结束"+System.DateTime.Now.ToString()); Console.WriteLine("执行结果是:SomeThingsNeedLitterOfTimeResult" + SomeThingsNeedLitterOfTimeResult); } private static string SaySomeThingsNeedLitterOfTime(string argsInput) { string reture = string.Empty; int cout=0; for (int i = 0; i < 10; i++) { Console.WriteLine("执行费时程序"+System.DateTime.Now.ToString()); Thread.Sleep(100); Console.WriteLine("执行费时程序"+System.DateTime.Now.ToString()); cout+=i; } reture = cout.ToString(); return reture; } } }