C#中异步回调函数演示源码
把代码过程经常用的内容做个珍藏,如下代码是关于C#中异步回调函数演示的代码,应该是对大伙有些用途。
static void Main(string[] args){ Func<string,string> showMessage = ShowMessage; IAsyncResult result = showMessage.BeginInvoke("测试异步委托", new AsyncCallback(Completed), null); while(!result.IsCompleted) { Console.WriteLine("主线程可以进行其它的操作!"); } Console.ReadLine();}static string ShowMessage(string x){ string current = string.Format("当前线程id为{0}",Thread.CurrentThread.ManagedThreadId); Thread.Sleep(3000); return string.Format("{0},输入为{1}", current, x);}static void Completed(IAsyncResult result){ Console.WriteLine("异步完成!"); AsyncResult _result = (AsyncResult) result; Func<string, string> showMessage = (Func<string, string>) _result.AsyncDelegate; Console.WriteLine(showMessage.EndInvoke(_result));}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。