• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    【源碼】c#如何使用多線程

    c#若何利用多線程,簡單幾步,讓你輕松幾步解決。

    東西/原料

    • IDE

    方式/步調

    1. 1

      1.  打開vs或者其他IDE, 建立節制臺項目如下

    2. 2

      2. 無參數的多線程。在Program.cs文件里面寫入如下代碼

      class Program

      {

      static void Main(string[] args)

      {

      int num = 2;

      for ( int i =0; i < num; i++)

      {

      noParmaThread();

      }

      }

      private static void StartThread()

      {

      Console.WriteLine("---------起頭了新線程---------");

      Thread.Sleep(2000);//wait

      Console.WriteLine("---------線程竣事---------");

      }

      //不需要傳遞參數

      private static void noParmaThread()

      {

      ThreadStart threadStart = new ThreadStart(StartThread);

      var thread = new Thread(threadStart);

      thread.Start();//起頭線程

      }

      }

    3. 3

      3 步調2的運行成果,如下圖

    4. 4

      4. 含參數多線程。

      static void Main(string[] args)

      {

      int num = 3;

      for ( int i =0; i < num; i++)

      {

      oneParamThread("第" + i.ToString() +"個");

      }

      }

      private static void oneParamThread( string param)

      {

      var thread = new Thread(new ParameterizedThreadStart(StartThread));

      thread.SetApartmentState(ApartmentState.STA);

      thread.IsBackground = true;

      thread.Start(param);

      thread.Join();

      }

      private static void StartThread(Object obj)

      {

      string s = obj as string;

      Console.WriteLine("---------起頭了 "+s +" 新線程---------");

      Thread.Sleep(2000);//wait

      Console.WriteLine("---------線程 "+s+" 竣事---------");

      }

    5. 5

      5. 步調4的運行成果如下圖所示

    6. 6

      6. 利用專門的線程類。代碼如下

      class Program

         {

             static void Main(string[] args)

             {

                 int num = 3;

                 for ( int i =0; i < num; i++)

                 {

                     MyThreadClass mt = new MyThreadClass(i);

                     ThreadStart threadStart = new ThreadStart(mt.StartThread);

                     Thread thread = new Thread(threadStart);

                     thread.Start();

                     //期待線程竣事

                 }

             }

         }

         public class MyThreadClass

         {

             public string output { set; get; }

             public int parame { set; get; }

             public MyThreadClass(int p)

             {

                 this.parame = p;

             }

             public void StartThread()

             {

                 Console.WriteLine("---------起頭了 " + parame.ToString() + " 新線程---------");

                 Thread.Sleep(2000);//wait

                 output = "線程 "+ parame+" 竣事了";

                 Console.WriteLine("---------線程 " + parame.ToString() + " 竣事---------");

             }

         }

    7. 7

      7. 步調6的成果如下圖

    8. 8

      若是您感覺有效,記得鄙人方點擊投票、點贊、存眷、留言,小編會按期送上更多的驚喜哦,您的撐持才是小編繼續盡力的動力,么么噠。

    • 發表于 2018-04-20 00:00
    • 閱讀 ( 1218 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆