最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# Thread類線程常用操作詳解

 更新時(shí)間:2021年03月12日 08:55:24   作者:UP技術(shù)控  
這篇文章主要介紹了c# Thread類線程常用操作詳解的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

創(chuàng)建線程

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

下面的程序演示了這個(gè)概念:

class ThreadCreationProgram
  {
    public static void CallToChildThread()
    {
      Console.WriteLine("Child thread starts");
    }
    
    static void Main(string[] args)
    {
      ThreadStart childref = new ThreadStart(CallToChildThread);
      Console.WriteLine("In Main: Creating the Child thread");
      Thread childThread = new Thread(childref);
      childThread.Start();
      Console.ReadKey();
    }
  }

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

In Main: Creating the Child thread
Child thread starts

管理線程

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

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

class ThreadCreationProgram
  {
    public static void CallToChildThread()
    {
      Console.WriteLine("Child thread starts");
      // 線程暫停 5000 毫秒
      int sleepfor = 5000;
      Console.WriteLine("Child Thread Paused for {0} seconds",
               sleepfor / 1000);
      Thread.Sleep(sleepfor);
      Console.WriteLine("Child thread resumes");
    }
    
    static void Main(string[] args)
    {
      ThreadStart childref = new ThreadStart(CallToChildThread);
      Console.WriteLine("In Main: Creating the Child thread");
      Thread childThread = new Thread(childref);
      childThread.Start();
      Console.ReadKey();
    }
  }

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

In Main: Creating the Child thread
Child thread starts
Child Thread Paused for 5 seconds
Child thread resumes

銷毀線程

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

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

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

class ThreadCreationProgram
  {
    public static void CallToChildThread()
    {
      try
      {

        Console.WriteLine("Child thread starts");
        // 計(jì)數(shù)到 10
        for (int counter = 0; counter <= 10; counter++)
        {
          Thread.Sleep(500);
          Console.WriteLine(counter);
        }
        Console.WriteLine("Child Thread Completed");

      }
      catch (ThreadAbortException e)
      {
        Console.WriteLine("Thread Abort Exception");
      }
      finally
      {
        Console.WriteLine("Couldn't catch the Thread Exception");
      }

    }
    
    static void Main(string[] args)
    {
      ThreadStart childref = new ThreadStart(CallToChildThread);
      Console.WriteLine("In Main: Creating the Child thread");
      Thread childThread = new Thread(childref);
      childThread.Start();
      // 停止主線程一段時(shí)間
      Thread.Sleep(2000);
      // 現(xiàn)在中止子線程
      Console.WriteLine("In Main: Aborting the Child thread");
      childThread.Abort();
      Console.ReadKey();
    }
  }

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

In Main: Creating the Child thread
Child thread starts
0
1
2
In Main: Aborting the Child thread
Thread Abort Exception
Couldn't catch the Thread Exception

以上就是c# Thread類線程常用操作詳解的詳細(xì)內(nèi)容,更多關(guān)于c# Thread類線程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文帶你了解C#中的協(xié)變與逆變

    一文帶你了解C#中的協(xié)變與逆變

    這篇文章介紹了C#中協(xié)變和逆變的相關(guān)知識(shí),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • C#中 城市線路圖的純算法以及附帶求極權(quán)值

    C#中 城市線路圖的純算法以及附帶求極權(quán)值

    本篇文章介紹了,在C#中城市線路圖的純算法以及附帶求極權(quán)值的方法,需要的朋友參考下
    2013-04-04
  • C#關(guān)鍵字之覆寫overwrite介紹

    C#關(guān)鍵字之覆寫overwrite介紹

    這篇文章介紹了C#關(guān)鍵字之覆寫overwrite,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#實(shí)現(xiàn)讀取txt文件生成Word文檔

    C#實(shí)現(xiàn)讀取txt文件生成Word文檔

    大家好,本篇文章主要講的是C#實(shí)現(xiàn)讀取txt文件生成Word文檔,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • 使用C#自制一個(gè)Windows安裝包的詳細(xì)過程

    使用C#自制一個(gè)Windows安裝包的詳細(xì)過程

    這篇文章主要介紹了如何使用C#自制一個(gè)Windows安裝包,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • WPF使用webView實(shí)現(xiàn)顯示瀏覽器網(wǎng)頁

    WPF使用webView實(shí)現(xiàn)顯示瀏覽器網(wǎng)頁

    在WPF中顯示一個(gè)可以操作的瀏覽器界面,你可以使用WebBrowser控件或WebView2控件,下面我們就來看看如何分別使用這兩個(gè)控件實(shí)現(xiàn)顯示瀏覽器網(wǎng)頁吧
    2025-01-01
  • C#實(shí)現(xiàn)IP攝像頭的方法

    C#實(shí)現(xiàn)IP攝像頭的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)IP攝像頭的方法,涉及C#IP連接與攝像頭視頻錄像的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 詳解C#如何使用消息隊(duì)列MSMQ

    詳解C#如何使用消息隊(duì)列MSMQ

    消息隊(duì)列 (MSMQ Microsoft Message Queuing)是MS提供的服務(wù),也就是Windows操作系統(tǒng)的功能,下面就跟隨小編一起了解一下C#中是如何使用消息隊(duì)列MSMQ的吧
    2024-01-01
  • C#配置文件操作類分享

    C#配置文件操作類分享

    這篇文章主要分享了C#配置文件操作類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#實(shí)現(xiàn)json格式數(shù)據(jù)解析功能的方法詳解

    C#實(shí)現(xiàn)json格式數(shù)據(jù)解析功能的方法詳解

    這篇文章主要介紹了C#實(shí)現(xiàn)json格式數(shù)據(jù)解析功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了C#解析json格式數(shù)據(jù)的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-12-12

最新評(píng)論

镇沅| 奉节县| 思南县| 阳城县| 车险| 涿鹿县| 秀山| 淮南市| 前郭尔| 客服| 云和县| 墨玉县| 水富县| 理塘县| 景宁| 延寿县| 郯城县| 布尔津县| 晋江市| 凭祥市| 抚远县| 绍兴市| 鄢陵县| 崇阳县| 元朗区| 任丘市| 广灵县| 庄河市| 绥滨县| 长治县| 宽甸| 互助| 延长县| 玉田县| 巍山| 澄江县| 邯郸市| 萨嘎县| 永济市| 建阳市| 卓资县|