threadplayaroundwithpriorities.cs

来自「介绍了.NET中线程的操作,线程的处理,优先级,同步等问题.」· CS 代码 · 共 50 行

CS
50
字号
using System;
using System.Threading;

namespace Wrox.ProCSharp.ThreadPlayaround
{
   class EntryPoint
   {
      static int interval;

      static void Main()
      {
         Console.Write("Interval to display results at?> ");
         interval = int.Parse(Console.ReadLine());

         Thread thisThread = Thread.CurrentThread;
         thisThread.Name = "Main Thread";

         ThreadStart workerStart = new ThreadStart(StartMethod);
         Thread workerThread = new Thread(workerStart);
         workerThread.Name = "Worker";
         workerThread.Priority = ThreadPriority.AboveNormal;
         workerThread.Start();

         DisplayNumbers();
         Console.WriteLine("Main Thread Finished");

         Console.ReadLine();

      }

      static void StartMethod()
      {
         DisplayNumbers();
         Console.WriteLine("Worker Thread Finished");
      }

      static void DisplayNumbers()
      {
         Thread thisThread = Thread.CurrentThread;
         string name = thisThread.Name;
         Console.WriteLine("Starting thread: " + name);
         Console.WriteLine(name + ": Current Culture = " + thisThread.CurrentCulture);
         for (int i=1 ; i<= 8*interval ; i++)
         {
            if (i%interval == 0)
               Console.WriteLine(name + ": count has reached " + i);
         }
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?