System.Threading.Thread的使用及傳遞參數等總結


 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace ConsoleApplication3
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //線程啟動One()
14             Thread t = new Thread(() => One());
15             t.Start();
16 
17             //線程啟動One(object p) 方法一(傳遞參數)
18             Thread tt = new Thread(() => One("mang"));
19             tt.Start();
20 
21             //線程啟動Two(object p) 方法二(傳遞參數)
22             Thread ttt = new Thread(new ParameterizedThreadStart(Two));
23             ttt.Start("mangmang");
24             Console.ReadLine();
25 
26             //構造函數
27             //new Thread(ThreadStart start)   初始化Thread類的新實例
28             //start   類型:System.Threading.ThreadStart
29             //        ThreadStart委托,它表示此線程開始執行時要調用的方法。
30         }
31 
32         static void One()
33         {
34             Console.WriteLine("One");
35         }
36 
37         static void One(object p)
38         {
39             Console.WriteLine("One's parameter is " + p.ToString());
40         }
41 
42         static void Two(object p)
43         {
44             Console.WriteLine("Two's parameter is " + p.ToString());
45         }
46     }
47 }

 Main(string[] args)是入口函數,是主線程,另外new了三個線程。一共四個線程

Thread.IsBackground=true/false;

前台線程(false):直到執行結束而結束。

后台線程(true):隨着主線程或者前台線程、本身線程的結束而結束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM