C#使用委托的形式調(diào)用線程代碼實(shí)例
委托
對(duì)于委托,我們都知道他是一個(gè)引用類型,具有引用類型所具有的通性。需要知道的是它保存的不是實(shí)際值,只是是保存對(duì)存儲(chǔ)在托管堆中的對(duì)象的引用?;蛘f的直接點(diǎn),委托就相當(dāng)于叫人幫忙,讓你幫你做一些事情。我這里就使用委托的形式,調(diào)用線程,來簡(jiǎn)單的說一下。
代碼如下:
using System;
using System.Threading;
namespace _012_線程
{
class Program
{
static void Main(string[] args) //在mian中線程是執(zhí)行一個(gè)線程里面的語句的執(zhí)行,是從上到下的
{
//通過委托 開啟一個(gè)線程
//==============可用泛型傳參數(shù)(無返回值)==============
Action threaA = ThreadTestA;
threaA.BeginInvoke(null,null); //開啟一個(gè)新的線程去執(zhí)行,threaA所引用的方法
Action<int> threaB = ThreadTestB;
threaB.BeginInvoke(111,null, null);
//可以認(rèn)為線程是同時(shí)執(zhí)行的 (異步執(zhí)行)
Console.WriteLine("異步執(zhí)行");
//================帶返回值的形式====================
//第一種方式 檢測(cè)線程結(jié)束 ----- IsCompleted線程是否行完畢
//Func<int, int> threaC = ThreadTestC;
////接收異步線程返回值
//IAsyncResult returnResult = threaC.BeginInvoke(111, null, null);
//while (!res.IsCompleted)
//{
// Console.Write(".");
// Thread.Sleep(10); //控制子線程的檢測(cè)頻率,(每10ms檢測(cè)一次)
//}
////取得異步線程返回值
//int result = threaC.EndInvoke(res);
//Console.WriteLine("IsCompleted方式檢測(cè):" + result);
//第二種方式 檢測(cè)線程結(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()方式檢測(cè):" + isEnd +" "+ result);
//第三種方式 檢測(cè)線程結(jié)束 ----- 通過回調(diào),檢測(cè)線程結(jié)束
Func<int,string, string> threaD = ThreadTestD;
//倒數(shù)第二個(gè)參數(shù),表示委托類型的參數(shù),(回調(diào)函數(shù))當(dāng)線程結(jié)束的時(shí)候會(huì)調(diào)用這個(gè)委托指向的方法
//最后一個(gè)參數(shù),用來給回調(diào)函數(shù)傳遞數(shù)據(jù)
IAsyncResult asy = threaD.BeginInvoke(111,"Czhenya", OnCallKey, threaD);
//改為L(zhǎng)amdba表達(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>
/// 一般是比較耗時(shí)的操作方法
/// </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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
List轉(zhuǎn)換成DataSet實(shí)現(xiàn)代碼
怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下2012-12-12
C#實(shí)現(xiàn)獲取運(yùn)行平臺(tái)系統(tǒng)信息的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取運(yùn)行平臺(tái)系統(tǒng)信息的方法,比較典型的C#應(yīng)用,需要的朋友可以參考下2014-07-07
c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例
本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來完成這個(gè)功能,大家參考使用吧2014-01-01
C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法
這篇文章主要介紹了C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法,涉及C#中await方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng)功能(不需要管理員權(quán)限)
在本文中,我們探討了如何使用C#語言實(shí)現(xiàn)應(yīng)用程序在系統(tǒng)啟動(dòng)時(shí)自動(dòng)運(yùn)行的功能,同時(shí)避免了對(duì)管理員權(quán)限的需求,文章通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04

