最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用委托的形式調(diào)用線程代碼實(shí)例

 更新時間:2018年10月26日 08:59:31   作者:Czhenya  
今天小編就為大家分享一篇關(guān)于C#使用委托的形式調(diào)用線程代碼實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

委托

對于委托,我們都知道他是一個引用類型,具有引用類型所具有的通性。需要知道的是它保存的不是實(shí)際值,只是是保存對存儲在托管堆中的對象的引用?;蛘f的直接點(diǎn),委托就相當(dāng)于叫人幫忙,讓你幫你做一些事情。我這里就使用委托的形式,調(diào)用線程,來簡單的說一下。

代碼如下:

using System;
using System.Threading;
namespace _012_線程
{
  class Program
  {
    static void Main(string[] args) //在mian中線程是執(zhí)行一個線程里面的語句的執(zhí)行,是從上到下的
    {
      //通過委托 開啟一個線程
      //==============可用泛型傳參數(shù)(無返回值)==============
      Action threaA = ThreadTestA;
      threaA.BeginInvoke(null,null); //開啟一個新的線程去執(zhí)行,threaA所引用的方法
      Action<int> threaB = ThreadTestB;
      threaB.BeginInvoke(111,null, null);
      //可以認(rèn)為線程是同時執(zhí)行的 (異步執(zhí)行)
      Console.WriteLine("異步執(zhí)行");
      //================帶返回值的形式====================
      //第一種方式 檢測線程結(jié)束  ----- IsCompleted線程是否行完畢
      //Func<int, int> threaC = ThreadTestC;
      ////接收異步線程返回值
      //IAsyncResult returnResult = threaC.BeginInvoke(111, null, null);
      //while (!res.IsCompleted)
      //{
      //  Console.Write(".");
      //  Thread.Sleep(10); //控制子線程的檢測頻率,(每10ms檢測一次)
      //}
      ////取得異步線程返回值
      //int result = threaC.EndInvoke(res);
      //Console.WriteLine("IsCompleted方式檢測:" + result);
      //第二種方式 檢測線程結(jié)束  -----  1000ms沒結(jié)束就返回false,反之
      Func<int, int> threaC = ThreadTestC;
      //接收異步線程返回值
      IAsyncResult returnResult = threaC.BeginInvoke(111, null, null);
      bool isEnd = returnResult.AsyncWaitHandle.WaitOne(1000);
      int result = 0;
      if (isEnd)
      {
        result = threaC.EndInvoke(returnResult);
      }
      Console.WriteLine("EndInvoke()方式檢測:" + isEnd +" "+ result);
      //第三種方式  檢測線程結(jié)束  ----- 通過回調(diào),檢測線程結(jié)束
      Func<int,string, string> threaD = ThreadTestD;
      //倒數(shù)第二個參數(shù),表示委托類型的參數(shù),(回調(diào)函數(shù))當(dāng)線程結(jié)束的時候會調(diào)用這個委托指向的方法
      //最后一個參數(shù),用來給回調(diào)函數(shù)傳遞數(shù)據(jù)
      IAsyncResult asy = threaD.BeginInvoke(111,"Czhenya", OnCallKey, threaD);
      //改為Lamdba表達(dá)式
      threaD.BeginInvoke(111, "Czhenya",(ar)=>{
        string res = threaD.EndInvoke(ar);
        Console.WriteLine("在Lamdba表達(dá)式中取得:"+res);
      },null);
      Console.ReadKey();
    }
    static void OnCallKey(IAsyncResult ar)
    {
      Func<int, string, string> thread = ar.AsyncState as Func<int, string, string>;
      string res = thread.EndInvoke(ar);
      Console.WriteLine("在回調(diào)函數(shù)中取到的結(jié)果 :"+res);
    }
    /// <summary>
    /// 一般是比較耗時的操作方法
    /// </summary>
    static void ThreadTestA()
    {
      Console.WriteLine("ThreaTestA");
    }
    static void ThreadTestB(int num)
    {
      Console.WriteLine("ThreaTestB "+num);
    }
    static int ThreadTestC(int num)
    {
      Console.WriteLine("ThreaTestC");
      Thread.Sleep(100); //讓當(dāng)前線程休眠(暫停線程(參數(shù)單位:ms))
      return num;
    }
    static string ThreadTestD(int num,string str)
    {
      Console.WriteLine("ThreaTestD");
      return num +" "+ str;
    }
  }
}

運(yùn)行結(jié)果圖:

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論

宝坻区| 晋城| 临清市| 富裕县| 和政县| 神池县| 奉节县| 宣威市| 和平区| 泰来县| 分宜县| 贵州省| 丹阳市| 武安市| 凯里市| 台前县| 弥勒县| 庆云县| 天津市| 民和| 龙川县| 南华县| 黔南| 林口县| 浦东新区| 长丰县| 洛阳市| 永康市| 休宁县| 玛沁县| 吉安县| 万安县| 北海市| 抚宁县| 西和县| 库伦旗| 集贤县| 珠海市| 定陶县| 荃湾区| 泰宁县|