ThreadPlayaround.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:1k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Threading;
  3. namespace Wrox.ProCSharp.ThreadPlayaround
  4. {
  5.    class EntryPoint
  6.    {
  7.       static int interval;
  8.       static void Main()
  9.       {
  10.          Console.Write("Interval to display results at?> ");
  11.          interval = int.Parse(Console.ReadLine());
  12.          Thread thisThread = Thread.CurrentThread;
  13.          thisThread.Name = "Main Thread";
  14.          ThreadStart workerStart = new ThreadStart(StartMethod);
  15.          Thread workerThread = new Thread(workerStart);
  16.          workerThread.Name = "Worker";
  17.          workerThread.Start();
  18.          DisplayNumbers();
  19.          Console.WriteLine("Main Thread Finished");
  20.          Console.ReadLine();
  21.       }
  22.       static void StartMethod()
  23.       {
  24.          DisplayNumbers();
  25.          Console.WriteLine("Worker Thread Finished");
  26.       }
  27.       static void DisplayNumbers()
  28.       {
  29.          Thread thisThread = Thread.CurrentThread;
  30.          string name = thisThread.Name;
  31.          Console.WriteLine("Starting thread: " + name);
  32.          Console.WriteLine(name + ": Current Culture = " + thisThread.CurrentCulture);
  33.          for (int i=1 ; i<= 8*interval ; i++)
  34.          {
  35.             if (i%interval == 0)
  36.                Console.WriteLine(name + ": count has reached " + i);
  37.          }
  38.       }
  39.    }
  40. }