c# System.Threading.Thread


 1 using System;
 2 using System.Threading;
 3 
 4 // Simple threading scenario:  Start a static method running
 5 // on a second thread.
 6 public class ThreadExample {
 7     // The ThreadProc method is called when the thread starts.
 8     // It loops ten times, writing to the console and yielding 
 9     // the rest of its time slice each time, and then ends.
10     public static void ThreadProc() {
11         for (int i = 0; i < 10; i++) {
12             Console.WriteLine("ThreadProc: {0}", i);
13             // Yield the rest of the time slice.
14             Thread.Sleep(0);
15         }
16     }
17 
18     public static void Main() {
19         Console.WriteLine("Main thread: Start a second thread.");
20         // The constructor for the Thread class requires a ThreadStart 
21         // delegate that represents the method to be executed on the 
22         // thread.  C# simplifies the creation of this delegate.
23         Thread t = new Thread(new ThreadStart(ThreadProc));
24 
25         // Start ThreadProc.  Note that on a uniprocessor, the new 
26         // thread does not get any processor time until the main thread 
27         // is preempted or yields.  Uncomment the Thread.Sleep that 
28         // follows t.Start() to see the difference.
29         t.Start();
30         //Thread.Sleep(0);
31 
32         for (int i = 0; i < 4; i++) {
33             Console.WriteLine("Main thread: Do some work.");
34             Thread.Sleep(0);
35         }
36 
37         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
38         t.Join();
39         Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
40         Console.ReadLine();
41     }
42 }

 

 

 1 using System;
 2 using System.Threading;
 3 
 4 // Simple threading scenario:  Start a static method running
 5 // on a second thread.
 6 public class ThreadExample {
 7     // The ThreadProc method is called when the thread starts.
 8     // It loops ten times, writing to the console and yielding 
 9     // the rest of its time slice each time, and then ends.
10     
11     private static ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
12     
13     public static void show(String thread_name, int run_count)
14     {
15         //cacheLock.EnterWriteLock();
16         
17         for (int i = 0; i < run_count; i++) {
18             Console.WriteLine("{0} runs {1}.", thread_name, i);
19             //Console.WriteLine("ThreadProc: {0}", i);
20             // Yield the rest of the time slice.
21             Thread.Sleep(0);
22         }
23         //cacheLock.ExitWriteLock();
24     }
25     
26     public static void ThreadProc() {
27         string name = Thread.CurrentThread.Name;
28         show(name, 10);
29         /*
30         cacheLock.EnterReadLock();
31         
32         Console.WriteLine("{0} runs.", name);
33         
34         for (int i = 0; i < 10; i++) {
35             
36             Console.WriteLine("ThreadProc: {0}", i);
37             // Yield the rest of the time slice.
38             Thread.Sleep(0);
39         }
40         cacheLock.ExitReadLock();*/
41     }
42 
43     public static void Main() {
44         Console.WriteLine("Main thread: Start a second thread.");
45         // The constructor for the Thread class requires a ThreadStart 
46         // delegate that represents the method to be executed on the 
47         // thread.  C# simplifies the creation of this delegate.
48         for (int i = 1; i < 5; i++)
49         {
50             Thread t = new Thread(ThreadProc);
51             t.Name = "Thread_" + i;
52             t.Start();
53         }
54         Thread.Sleep(250);
55         //Thread t = new Thread(new ThreadStart(ThreadProc));
56 
57         // Start ThreadProc.  Note that on a uniprocessor, the new 
58         // thread does not get any processor time until the main thread 
59         // is preempted or yields.  Uncomment the Thread.Sleep that 
60         // follows t.Start() to see the difference.
61         //t.Start();
62         //Thread.Sleep(1);
63 
64         for (int i = 0; i < 4; i++) {
65             Console.WriteLine("Main thread: Do some work.");
66             Thread.Sleep(0);
67         }
68 
69         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
70         //t.Join();
71         Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
72         Console.ReadLine();
73     }
74 }

 

構造函數 

Thread(ParameterizedThreadStart)

初始化 Thread 類的新實例,指定允許對象在線程啟動時傳遞給線程的委托。

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 類的新實例,指定允許對象在線程啟動時傳遞給線程的委托,並指定線程的最大堆棧大小。

Thread(ThreadStart)

初始化 Thread 類的新實例。

Thread(ThreadStart, Int32)

初始化 Thread 類的新實例,指定線程的最大堆棧大小。

屬性 

ApartmentState

獲取或設置此線程的單元狀態。

CurrentContext

獲取線程正在其中執行的當前上下文。

CurrentCulture

獲取或設置當前線程的區域性。

CurrentPrincipal

獲取或設置線程的當前負責人(對基於角色的安全性而言)。

CurrentThread

獲取當前正在運行的線程。

CurrentUICulture

獲取或設置資源管理器使用的當前區域性以便在運行時查找區域性特定的資源。

ExecutionContext

獲取 ExecutionContext 對象,該對象包含有關當前線程的各種上下文的信息。

IsAlive

獲取指示當前線程的執行狀態的值。

IsBackground

獲取或設置一個值,該值指示某個線程是否為后台線程。

IsThreadPoolThread

獲取指示線程是否屬於托管線程池的值。

ManagedThreadId

獲取當前托管線程的唯一標識符。

Name

獲取或設置線程的名稱。

Priority

獲取或設置指示線程的調度優先級的值。

ThreadState

獲取一個值,該值包含當前線程的狀態。

方法 

Abort()

在調用此方法的線程上引發 ThreadAbortException,以開始終止此線程的過程。 調用此方法通常會終止線程。

Abort(Object)

引發在其上調用的線程中的 ThreadAbortException 以開始處理終止線程,同時提供有關線程終止的異常信息。 調用此方法通常會終止線程。

AllocateDataSlot()

在所有線程上分配未命名的數據槽。 為了獲得更好的性能,請改用以 ThreadStaticAttribute 特性標記的字段。

AllocateNamedDataSlot(String)

在所有線程上分配已命名的數據槽。 為了獲得更好的性能,請改用以 ThreadStaticAttribute 特性標記的字段。

BeginCriticalRegion()

通知宿主執行將要進入一個代碼區域,在該代碼區域內線程中止或未經處理異常的影響可能會危害應用程序域中的其他任務。

BeginThreadAffinity()

通知主機托管代碼將要執行依賴於當前物理操作系統線程的標識的指令。

DisableComObjectEagerCleanup()

對於當前線程關閉運行時可調用包裝 (RCW) 的自動清理。

EndCriticalRegion()

通知宿主執行將要進入一個代碼區域,在該代碼區域內線程中止或未經處理異常的影響限於當前任務。

EndThreadAffinity()

通知宿主托管代碼已執行完依賴於當前物理操作系統線程的標識的指令。

Equals(Object)

確定指定的對象是否等於當前對象。

(Inherited from Object)
Finalize()

確保垃圾回收器回收 Thread 對象時釋放資源並執行其他清理操作。

FreeNamedDataSlot(String)

為進程中的所有線程消除名稱與槽之間的關聯。 為了獲得更好的性能,請改用以 ThreadStaticAttribute 特性標記的字段。

GetApartmentState()

返回表示單元狀態的 ApartmentState 值。

GetCompressedStack()

返回 CompressedStack 對象,此對象可用於獲取當前線程的堆棧。

GetData(LocalDataStoreSlot)

在當前線程的當前域中從當前線程上指定的槽中檢索值。 為了獲得更好的性能,請改用以 ThreadStaticAttribute 特性標記的字段。

GetDomain()

返回當前線程正在其中運行的當前域。

GetDomainID()

返回唯一的應用程序域標識符。

GetHashCode()

返回當前線程的哈希代碼。

GetNamedDataSlot(String)

查找命名的數據槽。 為了獲得更好的性能,請改用以 ThreadStaticAttribute 特性標記的字段。

GetType()

獲取當前實例的 Type

(Inherited from Object)
Interrupt()

中斷處於 WaitSleepJoin 線程狀態的線程。

Join()

在繼續執行標准的 COM 和 SendMessage 消息泵處理期間,阻止調用線程,直到由該實例表示的線程終止。

Join(Int32)

在繼續執行標准的 COM 和 SendMessage 消息泵處理期間,阻止調用線程,直到由該實例表示的線程終止或經過了指定時間為止。

Join(TimeSpan)

在繼續執行標准的 COM 和 SendMessage 消息泵處理期間,阻止調用線程,直到由該實例表示的線程終止或經過了指定時間為止。

MemberwiseClone()

創建當前 Object 的淺表副本。

(Inherited from Object)
MemoryBarrier()

按如下方式同步內存訪問:執行當前線程的處理器在對指令重新排序時,不能采用先執行 MemoryBarrier() 調用之后的內存存取,再執行 MemoryBarrier() 調用之前的內存存取的方式。

ResetAbort()

取消當前線程所請求的 Abort(Object)

Resume()

繼續已掛起的線程。

SetApartmentState(ApartmentState)

在線程啟動前設置其單元狀態。

SetCompressedStack(CompressedStack)

將捕獲的 CompressedStack 應用到當前線程。

SetData(LocalDataStoreSlot, Object)

在當前正在運行的線程上為此線程的當前域在指定槽中設置數據。 為了提高性能,請改用用 ThreadStaticAttribute 屬性標記的字段。

Sleep(Int32)

將當前線程掛起指定的毫秒數。

Sleep(TimeSpan)

將當前線程掛起指定的時間。

SpinWait(Int32)

導致線程等待由 iterations 參數定義的時間量。

Start()

導致操作系統將當前實例的狀態更改為 Running

Start(Object)

導致操作系統將當前實例的狀態更改為 Running,並選擇提供包含線程執行的方法要使用的數據的對象。

Suspend()

掛起線程,或者如果線程已掛起,則不起作用。

ToString()

返回表示當前對象的字符串。

(Inherited from Object)
TrySetApartmentState(ApartmentState)

在線程啟動前設置其單元狀態。

VolatileRead(Byte)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Double)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Int16)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Int32)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Int64)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(IntPtr)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Object)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(SByte)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(Single)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(UInt16)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(UInt32)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(UInt64)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileRead(UIntPtr)

讀取字段值。 無論處理器的數目或處理器緩存的狀態如何,該值都是由計算機的任何處理器寫入的最新值。

VolatileWrite(Byte, Byte)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Double, Double)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Int16, Int16)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Int32, Int32)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Int64, Int64)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(IntPtr, IntPtr)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Object, Object)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(SByte, SByte)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(Single, Single)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(UInt16, UInt16)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(UInt32, UInt32)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(UInt64, UInt64)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

VolatileWrite(UIntPtr, UIntPtr)

立即向字段寫入一個值,以使該值對計算機中的所有處理器都可見。

Yield()

導致調用線程執行准備好在當前處理器上運行的另一個線程。 由操作系統選擇要執行的線程。


免責聲明!

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



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