日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
聊聊Thread類線程常用操作

本文轉(zhuǎn)載自微信公眾號「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請聯(lián)系UP技術(shù)控公眾號。

伊吾網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

創(chuàng)建線程

線程是通過擴(kuò)展 Thread 類創(chuàng)建的。擴(kuò)展的 Thread 類調(diào)用 Start() 方法來開始子線程的執(zhí)行。

下面的程序演示了這個概念:

 
 
 
 
  1. class ThreadCreationProgram
  2.     {
  3.         public static void CallToChildThread()
  4.         {
  5.             Console.WriteLine("Child thread starts");
  6.         }
  7.        
  8.         static void Main(string[] args)
  9.         {
  10.             ThreadStart childref = new ThreadStart(CallToChildThread);
  11.             Console.WriteLine("In Main: Creating the Child thread");
  12.             Thread childThread = new Thread(childref);
  13.             childThread.Start();
  14.             Console.ReadKey();
  15.         }
  16.     }

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

 
 
 
 
  1. In Main: Creating the Child thread
  2. Child thread starts

管理線程

Thread 類提供了各種管理線程的方法。

下面的實(shí)例演示了 sleep() 方法的使用,用于在一個特定的時間暫停線程。

 
 
 
 
  1. class ThreadCreationProgram
  2.     {
  3.         public static void CallToChildThread()
  4.         {
  5.             Console.WriteLine("Child thread starts");
  6.             // 線程暫停 5000 毫秒
  7.             int sleepfor = 5000;
  8.             Console.WriteLine("Child Thread Paused for {0} seconds",
  9.                               sleepfor / 1000);
  10.             Thread.Sleep(sleepfor);
  11.             Console.WriteLine("Child thread resumes");
  12.         }
  13.        
  14.         static void Main(string[] args)
  15.         {
  16.             ThreadStart childref = new ThreadStart(CallToChildThread);
  17.             Console.WriteLine("In Main: Creating the Child thread");
  18.             Thread childThread = new Thread(childref);
  19.             childThread.Start();
  20.             Console.ReadKey();
  21.         }
  22.     }

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

 
 
 
 
  1. In Main: Creating the Child thread
  2. Child thread starts
  3. Child Thread Paused for 5 seconds
  4. Child thread resumes

銷毀線程

Abort() 方法用于銷毀線程。

通過拋出 threadabortexception 在運(yùn)行時中止線程。這個異常不能被捕獲,如果有 finally 塊,控制會被送至 finally 塊。

下面的程序說明了這點(diǎn):

 
 
 
 
  1. class ThreadCreationProgram
  2.     {
  3.         public static void CallToChildThread()
  4.         {
  5.             try
  6.             {
  7.                 Console.WriteLine("Child thread starts");
  8.                 // 計數(shù)到 10
  9.                 for (int counter = 0; counter <= 10; counter++)
  10.                 {
  11.                     Thread.Sleep(500);
  12.                     Console.WriteLine(counter);
  13.                 }
  14.                 Console.WriteLine("Child Thread Completed");
  15.             }
  16.             catch (ThreadAbortException e)
  17.             {
  18.                 Console.WriteLine("Thread Abort Exception");
  19.             }
  20.             finally
  21.             {
  22.                 Console.WriteLine("Couldn't catch the Thread Exception");
  23.             }
  24.         }
  25.        
  26.         static void Main(string[] args)
  27.         {
  28.             ThreadStart childref = new ThreadStart(CallToChildThread);
  29.             Console.WriteLine("In Main: Creating the Child thread");
  30.             Thread childThread = new Thread(childref);
  31.             childThread.Start();
  32.             // 停止主線程一段時間
  33.             Thread.Sleep(2000);
  34.             // 現(xiàn)在中止子線程
  35.             Console.WriteLine("In Main: Aborting the Child thread");
  36.             childThread.Abort();
  37.             Console.ReadKey();
  38.         }
  39.     }

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

 
 
 
 
  1. In Main: Creating the Child thread
  2. Child thread starts
  3. 0
  4. 1
  5. 2
  6. In Main: Aborting the Child thread
  7. Thread Abort Exception
  8. Couldn't catch the Thread Exception

文章標(biāo)題:聊聊Thread類線程常用操作
文章轉(zhuǎn)載:http://www.dlmjj.cn/article/dpisdoe.html