ThreadPlayaroundWithPriorities.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.Priority = ThreadPriority.AboveNormal;
  18.          workerThread.Start();
  19.          DisplayNumbers();
  20.          Console.WriteLine("Main Thread Finished");
  21.          Console.ReadLine();
  22.       }
  23.       static void StartMethod()
  24.       {
  25.          DisplayNumbers();
  26.          Console.WriteLine("Worker Thread Finished");
  27.       }
  28.       static void DisplayNumbers()
  29.       {
  30.          Thread thisThread = Thread.CurrentThread;
  31.          string name = thisThread.Name;
  32.          Console.WriteLine("Starting thread: " + name);
  33.          Console.WriteLine(name + ": Current Culture = " + thisThread.CurrentCulture);
  34.          for (int i=1 ; i<= 8*interval ; i++)
  35.          {
  36.             if (i%interval == 0)
  37.                Console.WriteLine(name + ": count has reached " + i);
  38.          }
  39.       }
  40.    }
  41. }