成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

簡單易懂的C#.NET多線程應用

開發 后端
本文從啟動線程、殺死線程、暫停線程、優先級、掛起線程、恢復線程等方面介紹了C#.NET多線程應用。希望對大家有所幫助。

 .NET將關于多線程的功能定義在System.Threading名字空間中。因此,要實現C#.NET多線程應用,必須先聲明引用此名字空間(using System.Threading;)。

即使你沒有編寫c#.net多線程應用程序的經驗,也可能聽說過“啟動線程”“殺死線程”這些詞,其實除了這兩個外,涉及多線程方面的還有諸如“暫停線程”“優先級”“掛起線程”“恢復線程”等等。下面將一個一個的解釋。

a.啟動線程

顧名思義,“啟動線程”就是新建并啟動一個線程的意思,如下代碼可實現:   

  1. Thread thread1 = new Thread(new ThreadStart( Count));  

其中的 Count 是將要被新線程執行的函數。

b.殺死線程

“殺死線程”就是將一線程斬草除根,為了不白費力氣,在殺死一個線程前***先判斷它是否還活著(通過 IsAlive 屬性),然后就可以調用 Abort 方法來殺死此線程。

c.暫停線程

它的意思就是讓一個正在運行的線程休眠一段時間。如 thread.Sleep(1000); 就是讓線程休眠1秒鐘。

d.優先級

這個用不著解釋了。Thread類中有一個ThreadPriority屬性,它用來設置優先級,但不能保證操作系統會接受該優先級。一個線程的優先級可分為5種:Normal, AboveNormal, BelowNormal, Highest, Lowest。具體實現例子如下:  

  1. thread.Priority = ThreadPriority.Highest; 

e.掛起線程

Thread類的Suspend方法用來掛起線程,知道調用Resume,此線程才可以繼續執行。如果線程已經掛起,那就不會起作用。

  1. if (thread.ThreadState = ThreadState.Running)  
  2. {  
  3.      thread.Suspend();  

f.恢復線程

用來恢復已經掛起的線程,以讓它繼續執行,如果線程沒掛起,也不會起作用。

  1. if (thread.ThreadState = ThreadState.Suspended)  
  2. {  
  3.      thread.Resume();  

下面將列出一個例子,以說明簡單的線程處理功能。此例子來自于幫助文檔。

  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.         // Start ThreadProc.  On a uniprocessor, the thread does not get  
  25.         // any processor time until the main thread yields.  Uncomment  
  26.         // the Thread.Sleep that follows t.Start() to see the difference.  
  27.         t.Start();  
  28.         //Thread.Sleep(0);  
  29.  
  30.         for (int i = 0; i <  4; i++) {  
  31.             Console.WriteLine("Main thread: Do some work.");  
  32.             Thread.Sleep(0);  
  33.         }  
  34.  
  35.         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");  
  36.         t.Join();  
  37.         Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");  
  38.         Console.ReadLine();  
  39.     }  

此代碼產生的輸出類似如下內容:

  1. Main thread: Start a second thread.  
  2. Main thread: Do some work.  
  3. ThreadProc: 0  
  4. Main thread: Do some work.  
  5. ThreadProc: 1  
  6. Main thread: Do some work.  
  7. ThreadProc: 2  
  8. Main thread: Do some work.  
  9. ThreadProc: 3  
  10. Main thread: Call Join(), to wait until ThreadProc ends.  
  11. ThreadProc: 4  
  12. ThreadProc: 5  
  13. ThreadProc: 6  
  14. ThreadProc: 7  
  15. ThreadProc: 8  
  16. ThreadProc: 9  
  17. Main thread: ThreadProc.Join has returned.  Press Enter to end program. 

C#.NET多線程應用的相關知識就介紹到這里,是不是非常簡單呢?

【編輯推薦】

  1. 淺析C#啟動停止SQL數據庫服務之方法
  2. 總結C#獲取當前路徑的7種方法
  3. 淺析C# treeview控件的使用方法
  4. C#多態性的概念及其應用
  5. 介紹C#構造函數的使用方法
責任編輯:book05 來源: 新浪博客
相關推薦

2009-08-24 16:30:43

C#.NET綁定Off

2009-09-11 11:30:53

Net60C#.NET

2011-06-17 15:55:19

ArrayListC#

2009-08-25 13:53:20

C#.NET rege

2009-08-26 14:23:14

C#.Net Fram

2009-08-13 10:35:55

C#.NET操作XML

2009-08-24 16:19:54

C#.NET綁定Off

2009-08-26 10:09:22

C#編碼規范

2009-08-19 15:44:09

ObjectARX .

2009-04-02 15:21:43

c#IDisposeFinalize

2024-10-14 16:25:59

C#線程鎖代碼

2009-10-09 17:01:32

VB.NET多線程

2009-10-20 10:23:08

VB.NET多線程編程

2009-09-01 17:15:42

C#多線程應用

2009-08-19 16:19:33

Employee對象

2009-09-01 16:14:05

ArrayList與A

2021-05-27 08:47:16

C語言C語言程序開發

2009-08-19 16:05:46

AutoCADEditor類

2009-08-28 09:29:02

2009-10-27 12:20:06

VB.NET多線程應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区观看| 久久噜噜噜精品国产亚洲综合 | 国产成人一区二区三区 | 日韩视频精品 | 手机日韩 | 五月婷婷丁香 | 亚洲精品久| 欧美一区二区三区在线看 | 天天插天天射天天干 | 国产精品久久久久久久久久久久午夜片 | 国产精品精品视频一区二区三区 | 69性欧美高清影院 | 91精品久久久久久久久中文字幕 | 亚洲有码转帖 | 国产一二三视频在线观看 | 成人一区二区三区在线 | 成人免费小视频 | 日韩精品一区二区三区中文在线 | 草樱av | 日韩精品专区在线影院重磅 | 欧美在线观看一区二区 | 91福利在线导航 | 91精品国产色综合久久 | 国精产品一区二区三区 | 国产激情网站 | 亚洲视频欧美视频 | 午夜视频免费网站 | 日韩精品在线看 | 麻豆av片 | 日韩毛片视频 | 91热在线| 99这里只有精品视频 | 一级黄a| 91久久精品一区二区三区 | 黄免费看 | 亚州av在线 | 色综网 | 91精品国产91综合久久蜜臀 | 亚洲欧美一区二区三区在线 | 天堂av中文 | 亚洲精品一 |