C#多線程基礎,適合新手了解
一、創建線程
在整個系列文章中,我們主要使用Visual Studio 2015作為線程編程的主要工具。在C#語言中創建、使用線程只需要按以下步驟編寫即可:
1、啟動Visual Studio 2016,新建一個控制臺應用程序。
2、確保該控制臺程序使用.NET Framework 4.6或以上版本。然而在該篇中的所有示例使用較低版本可以正常工作。
3、雙擊打開該控制臺應用程序中的“Program.cs”文件,在其中編寫如下代碼:
using System; using System.Threading; using static System.Console; namespaceRecipe01 { class Program { static void PrintNumbers() { WriteLine("Starting..."); for (int i = 1; i < 10; i++) { WriteLine(i); } } static void Main(string[] args) { Thread t= new Thread(PrintNumbers); t.Start(); PrintNumbers(); } } }
在第2行代碼處,我們導入了System.Threading命名空間,該命名空間包含了我們編寫多線程程序所需要的所有類型。
在第3行代碼處,我們使用了C# 6.0的using static特性,使用了該特性之後,在代碼中允許我們在使用System.Console類型的靜態方法的時候不需要指定其類型名。
在第9~16行代碼處,我們定義了一個名為“PrintNumbers”的方法,該方法將在“Main”方法和線程中進行調用。
在第20行代碼處,我們創建了一個線程來運行“PrintNumbers”方法,當我們初始化一個線程時,一個“ThreadStart”或“ParameterizedThreadStart”委托的實例被傳遞給線程的構造方法。
在第21行代碼處,我們啟動線程。
在第22行代碼處,我們在“Main”方法中調用“PrintNumbers”方法。
4、運行該控制臺應用程序,運行效果(每次運行效果可能不同)如下圖所示:
二、中止線程
在這一節,我們將讓線程等待一些時間,在等待的這段時間內,該線程不會消耗操作系統的資源。編寫步驟如下:
1、使用Visual Studio 2015創建一個新的控制臺應用程序。
2、雙擊打開“Program.cs”文件,編寫代碼如下所示:
using System; using System.Threading; using static System.Console; namespace Recipe01 { class Program { static void PrintNumbers() { WriteLine("Starting..."); for (int i = 1; i < 10; i++) { WriteLine(i); } } static void Main(string[] args) { Thread t = new Thread(PrintNumbers); t.Start(); PrintNumbers(); } } }
3、運行該控制臺應用程序,運行效果(每次運行效果可能不同)如下圖所示:
三、線程等待
在這一節中,我們將講述如何在一個線程執行完畢後,再執行剩余的代碼,要完成這個工作,我們不能使用Thread.Sleep方法,因為我們不知道另一個線程精確的執行時間。要使一個線程等待另一個線程執行完畢後再進行其他工作,只需要按下列步驟編寫代碼即可:
1、使用Visual Studio 2015創建一個新的控制臺應用程序。
2、雙擊打開“Program.cs”文件,編寫如下代碼:
using System; using System.Threading; using static System.Console; using static System.Threading.Thread; namespace Recipe03 { class Program { static void PrintNumbersWithDelay() { WriteLine("Starting..."); for(int i = 1; i < 10; i++) { Sleep(TimeSpan.FromSeconds(2)); WriteLine(i); } } static void Main(string[] args) { WriteLine("Starting..."); Thread t = new Thread(PrintNumbersWithDelay); t.Start(); t.Join(); WriteLine("Thread completed"); } } }
3、運行該控制臺應用程序,運行效果如下圖所示:
在第26行代碼處,我們在“Main”方法中調用調用“t.Join”方法,該方法允許我們等待線程t執行完畢後,再執行“Main”方法中剩余的代碼。有了該技術,我們可以同步兩個線程的執行步驟。第一個線程等待第二個線程執行完畢後,再進行其他的工作,在第一個線程等待期間,第一個線程的狀態為“bolcked”狀態,和我們調用Thread.Sleep的狀態一樣。
四、終止線程
在這一節中,我們將講述如何終止另一個線程的執行。步驟如下:
1、使用Visual Studio 2015創建一個新的控制臺應用程序。
2、雙擊打開“Program.cs”文件,編寫如下代碼:
using System; using System.Threading; using static System.Console; using static System.Threading.Thread; namespace Recipe04 { class Program { static void PrintNumbers() { WriteLine("Starting..."); for (int i = 1; i < 10; i++) { WriteLine(i); } } static void PrintNumbersWithDelay() { WriteLine("Starting..."); for (int i = 1; i < 10; i++) { Sleep(TimeSpan.FromSeconds(2)); WriteLine(i); } } static void Main(string[] args) { WriteLine("Starting program..."); Thread t = new Thread(PrintNumbersWithDelay); t.Start(); Thread.Sleep(TimeSpan.FromSeconds(6)); t.Abort(); WriteLine("A thread has been aborted"); t = new Thread(PrintNumbers); t.Start(); PrintNumbers(); } } }
3、運行該控制臺應用程序,運行效果(每次運行效果可能不同)如下圖所示:
C#多線程基礎,適合新手了解