欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中異步回調(diào)函數(shù)用法實(shí)例

 更新時(shí)間:2015年04月22日 10:27:59   作者:dingding  
這篇文章主要介紹了C#中異步回調(diào)函數(shù)用法,實(shí)例分析了異步回調(diào)函數(shù)的定義及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#中異步回調(diào)函數(shù)用法。分享給大家供大家參考。具體如下:

static void Main(string[] args)
{
 Func<string,string> showMessage = ShowMessage;
 //設(shè)置了回調(diào)函數(shù)Completed,不能有返回值
 IAsyncResult result = showMessage.BeginInvoke("測(cè)試異步委托",new AsyncCallback(Completed),null);
 //半段異步是否結(jié)束
 while(!result.IsCompleted)
 {
  Console.WriteLine("主線程可以進(jìn)行其它的操作!");
 }
 Console.ReadLine();
}
static string ShowMessage(string x)
{
 string current = string.Format("當(dāng)前線程id為{0}",Thread.CurrentThread.ManagedThreadId);
 Thread.Sleep(3000);
 return string.Format("{0},輸入為{1}", current, x);
}
static void Completed(IAsyncResult result)
{
 Console.WriteLine("異步完成!");
 //獲取委托對(duì)象,并用EndInvoke方法獲取返回結(jié)果
 AsyncResult _result = (AsyncResult) result;
 Func<string, string> showMessage = (Func<string, string>) _result.AsyncDelegate;
 //結(jié)束異步操作并輸出
 Console.WriteLine(showMessage.EndInvoke(_result));
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論