国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#使用委托的形式調用線程代碼實例

2020-01-24 00:13:21
字體:
來源:轉載
供稿:網友

委托

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

代碼如下:

using System;using System.Threading;namespace _012_線程{  class Program  {    static void Main(string[] args) //在mian中線程是執行一個線程里面的語句的執行,是從上到下的    {      //通過委托 開啟一個線程      //==============可用泛型傳參數(無返回值)==============      Action threaA = ThreadTestA;      threaA.BeginInvoke(null,null); //開啟一個新的線程去執行,threaA所引用的方法      Action<int> threaB = ThreadTestB;      threaB.BeginInvoke(111,null, null);      //可以認為線程是同時執行的 (異步執行)      Console.WriteLine("異步執行");      //================帶返回值的形式====================      //第一種方式 檢測線程結束  ----- 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);      //第二種方式 檢測線程結束  -----  1000ms沒結束就返回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);      //第三種方式  檢測線程結束  ----- 通過回調,檢測線程結束      Func<int,string, string> threaD = ThreadTestD;      //倒數第二個參數,表示委托類型的參數,(回調函數)當線程結束的時候會調用這個委托指向的方法      //最后一個參數,用來給回調函數傳遞數據      IAsyncResult asy = threaD.BeginInvoke(111,"Czhenya", OnCallKey, threaD);      //改為Lamdba表達式      threaD.BeginInvoke(111, "Czhenya",(ar)=>{        string res = threaD.EndInvoke(ar);        Console.WriteLine("在Lamdba表達式中取得:"+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("在回調函數中取到的結果 :"+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); //讓當前線程休眠(暫停線程(參數單位:ms))      return num;    }    static string ThreadTestD(int num,string str)    {      Console.WriteLine("ThreaTestD");      return num +" "+ str;    }  }}

運行結果圖:

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高阳县| 福海县| 师宗县| 中西区| 株洲市| 林口县| 东辽县| 宁国市| 锦屏县| 光山县| 瑞丽市| 措美县| 万盛区| 乐陵市| 永清县| 黑龙江省| 承德县| 任丘市| 永新县| 奉新县| 孟津县| 怀仁县| 定南县| 开封市| 富源县| 饶河县| 大连市| 天峻县| 北票市| 河源市| 永定县| 通州市| 宁阳县| 崇仁县| 通辽市| 光山县| 庄河市| 新安县| 满城县| 松原市| 裕民县|