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 類的新實例,指定容許對象在線程啓動時傳遞給線程的委託。api

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 類的新實例,指定容許對象在線程啓動時傳遞給線程的委託,並指定線程的最大堆棧大小。緩存

Thread(ThreadStart)

初始化 Thread 類的新實例。安全

Thread(ThreadStart, Int32)

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

屬性 

ApartmentState

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

CurrentContext

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

CurrentCulture

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

CurrentPrincipal

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

CurrentThread

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

CurrentUICulture

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

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()

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

相關文章
相關標籤/搜索