(1)不需要傳遞參數,也不需要回傳參數
ThreadStart是一個委託,這個委託的定義為void ThreadStart(),沒有參數與回傳值。
class Program { static void Main(string[] args) { for (int i = 0; i < 30; i++) { ThreadStart threadStart = new ThreadStart(Calculate); Thread thread = new Thread(threadStart); thread.Start(); } Thread.Sleep(2000); Console.Read(); } public static void Calculate() { DateTime time = DateTime.Now;//得到当前时间 Random ra = new Random();//随机数对象 Thread.Sleep(ra.Next(10,100));//随机休眠一段时间 Console.WriteLine(time.Minute + ":" + time.Millisecond); } }
(2)需要傳遞單一參數
ParameterThreadStart委託定義為void ParameterizedThreadStart(object state),有一個參數但沒有回傳值。
class Program { static void Main(string[] args) { for (int i = 0; i < 30; i++) { ParameterizedThreadStart tStart = new ParameterizedThreadStart(Calculate); Thread thread = new Thread(tStart); thread.Start(i*10+10);//传递参数 } Thread.Sleep(2000); Console.Read(); } public static void Calculate(object arg) { Random ra = new Random();//随机数对象 Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间 Console.WriteLine(arg); } }
(3)使用專門的線程類別(常用)
使用線程類別可以有多個參數與多個返回值,十分靈活!
class Program { static void Main(string[] args) { MyThread mt = new MyThread(100); ThreadStart threadStart = new ThreadStart(mt.Calculate); Thread thread = new Thread(threadStart); thread.Start(); //等待线程结束 while (thread.ThreadState != ThreadState.Stopped) { Thread.Sleep(10); } Console.WriteLine(mt.Result);//打印返回值 Console.Read(); } } public class MyThread//线程类 { public int Parame { set; get; }//参数 public int Result { set; get; }//返回值 //构造函数 public MyThread(int parame) { this.Parame = parame; } //线程执行方法 public void Calculate() { Random ra = new Random();//随机数对象 Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间 Console.WriteLine(this.Parame); this.Result = this.Parame * ra.Next(10, 100); } }
(4)使用匿名方法(常用)
使用匿名方法啟動執行緒可以有多個參數和回傳值,而且使用非常方便!
class Program { static void Main(string[] args) { int Parame = 100;//当做参数 int Result = 0;//当做返回值 //匿名方法 ThreadStart threadStart = new ThreadStart(delegate() { Random ra = new Random();//随机数对象 Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间 Console.WriteLine(Parame);//输出参数 Result = Parame * ra.Next(10, 100);//计算返回值 }); Thread thread = new Thread(threadStart); thread.Start();//多线程启动匿名方法 //等待线程结束 while (thread.ThreadState != ThreadState.Stopped) { Thread.Sleep(10); } Console.WriteLine(Result);//打印返回值 Console.Read(); } }
(5)使用委託開啟多執行緒(多執行緒深入)
1、用委託(Delegate)的BeginInvoke和EndInvoke方法操作執行緒
BeginInvoke方法可以使用執行緒非同步地執行委託所指向的方法。然後透過EndInvoke方法得到方法的回傳值(EndInvoke方法的回傳值就是被呼叫方法的回傳值),或是確定方法已經被成功呼叫。
class Program { private delegate int NewTaskDelegate(int ms); private static int newTask(int ms) { Console.WriteLine("任务开始"); Thread.Sleep(ms); Random random = new Random(); int n = random.Next(10000); Console.WriteLine("任务完成"); return n; } static void Main(string[] args) { NewTaskDelegate task = newTask; IAsyncResult asyncResult = task.BeginInvoke(2000, null, null); //EndInvoke方法将被阻塞2秒 int result = task.EndInvoke(asyncResult); Console.WriteLine(result); Console.Read(); } }
2、使用IAsyncResult.IsCompleted屬性來判斷非同步呼叫是否完成
class Program { private delegate int NewTaskDelegate(int ms); private static int newTask(int ms) { Console.WriteLine("任务开始"); Thread.Sleep(ms); Random random = new Random(); int n = random.Next(10000); Console.WriteLine("任务完成"); return n; } static void Main(string[] args) { NewTaskDelegate task = newTask; IAsyncResult asyncResult = task.BeginInvoke(2000, null, null); //等待异步执行完成 while (!asyncResult.IsCompleted) { Console.Write("*"); Thread.Sleep(100); } // 由于异步调用已经完成,因此, EndInvoke会立刻返回结果 int result = task.EndInvoke(asyncResult); Console.WriteLine(result); Console.Read(); } }
3、使用WaitOne方法等待非同步方法執行完成
WaitOne的第一個參數表示要等待的毫秒數,在指定時間之內, WaitOne方法將等待,直到非同步呼叫完成,並發出通知,WaitOne方法才會傳回true。當等待指定時間之後,非同步呼叫仍未完成,WaitOne方法傳回false,如果指定時間為0,表示不等待,如果為-1,表示永遠等待,直到非同步呼叫完成。
class Program { private delegate int NewTaskDelegate(int ms); private static int newTask(int ms) { Console.WriteLine("任务开始"); Thread.Sleep(ms); Random random = new Random(); int n = random.Next(10000); Console.WriteLine("任务完成"); return n; } static void Main(string[] args) { NewTaskDelegate task = newTask; IAsyncResult asyncResult = task.BeginInvoke(2000, null, null); //等待异步执行完成 while (!asyncResult.AsyncWaitHandle.WaitOne(100, false)) { Console.Write("*"); } int result = task.EndInvoke(asyncResult); Console.WriteLine(result); Console.Read(); } }
4、使用回呼方式回傳結果
要注意的是“my.BeginInvoke(3,300, MethodCompleted, my)”,BeginInvoke方法的參數傳遞方式:
前面一部分(3,300)是其委託本身的參數。
倒數第二個參數(MethodCompleted)是回呼方法委託類型,他是回呼方法的委託,此委託沒有回傳值,有一個IAsyncResult類型的參數,當method方法執行完後,系統會自動呼叫MethodCompleted方法。
最後一個參數(my)需要向MethodCompleted方法中傳遞一些值,一般可以傳遞被呼叫方法的委託,這個值可以使用IAsyncResult.AsyncState屬性來獲得。
class Program { private delegate int MyMethod(int second, int millisecond); //线程执行方法 private static int method(int second, int millisecond) { Console.WriteLine("线程休眠" + (second * 1000 + millisecond) + "毫秒"); Thread.Sleep(second * 1000 + millisecond); Random random = new Random(); return random.Next(10000); } //回调方法 private static void MethodCompleted(IAsyncResult asyncResult) { if (asyncResult == null || asyncResult.AsyncState == null) { Console.WriteLine("回调失败!!!"); return; } int result = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult); Console.WriteLine("任务完成,结果:" + result); } static void Main(string[] args) { MyMethod my = method; IAsyncResult asyncResult = my.BeginInvoke(3,300, MethodCompleted, my); Console.WriteLine("任务开始"); Console.Read(); } }
5、其他元件的BeginXXX和EndXXX方法
在其他的.net元件中也有類似BeginInvoke和EndInvoke的方法,如System.Net.HttpWebRequest類別的BeginGetResponse和EndGetResponse方法。其使用方法類似委託類型的BeginInvoke和EndInvoke方法,例如:
class Program { //回调函数 private static void requestCompleted(IAsyncResult asyncResult) { if (asyncResult == null || asyncResult.AsyncState==null) { Console.WriteLine("回调失败"); return; } HttpWebRequest hwr = asyncResult.AsyncState as HttpWebRequest; HttpWebResponse response = (HttpWebResponse)hwr.EndGetResponse(asyncResult); StreamReader sr = new StreamReader(response.GetResponseStream()); string str = sr.ReadToEnd(); Console.WriteLine("返回流长度:"+str.Length); } static void Main(string[] args) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baidu.com"); //异步请求 IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request); Console.WriteLine("任务开始"); Console.Read(); } }

本文解釋瞭如何使用printf中的\ n逃脫序列在C中創建新線字符並列出函數。 它詳細介紹了功能並提供了代碼示例,以說明其用於輸出中的線路斷裂。

本文探討了C中的無指針啟用的挑戰。它認為問題本身不是零,而是濫用。 本文詳細介紹了預防退出的最佳實踐,包括提出前檢查,指針pitiberi

本文指導初學者選擇C編譯器。 它認為,海灣合作委員會由於其易用性,廣泛的可用性和廣泛的資源,最適合初學者。 但是,它也比較了海灣室,Clang,MSVC和TCC,突出了它們的差異

本文強調了NULL在現代C編程中的持續重要性。 儘管取得了進步,但NULL對於明確的指針管理仍然至關重要,從而通過標記沒有有效的內存地址來防止細分故障。 最好的prac

本文回顧了初學者的在線C編譯器,重點是易用性和調試功能。 在線GDB和REPL。 其他選項,例如Programiz和Compil

本文討論了C IDE中的有效代碼複製。 它強調,複製是IDE功能,而不是編譯器功能,並且詳細提高了效率的策略,包括使用IDE選擇工具,代碼折疊,搜索/替換,Templa

本文比較在線C編程平台,突出了諸如調試工具,IDE功能,標準合規性和內存/執行限制等功能的差異。 它認為“最佳”平台取決於用戶需求

該教程通過在Windows,MacOS和Linux上安裝C編譯器來指導用戶。 它詳細介紹了流行編譯器(Mingw,Visual Studio,Xcode,GCC)的安裝,解釋了環境變量配置,並提供故障排除步驟


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。