⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
using System;
using System.Threading;

namespace UsingThreads
{
   public class MyThread
   {
      private string data;

      public MyThread(string data)
      {
         this.data = data;
      }

      public void ThreadMain()
      {
         Console.WriteLine("Running in a thread, data: {0}", data);
      }
   }

   public struct Data
   {
      public string Message;
   }

   class Program
   {
      static void Main()
      {
         Thread t1 = new Thread(Prio);
         t1.Name = "First";

         Thread t2 = new Thread(Prio);
         t2.Name = "Second";
         t1.Priority = ThreadPriority.Highest;
         t2.Priority = ThreadPriority.Lowest;

         t1.Start();
         t2.Start();

         //Thread t1 = new Thread(ThreadMain);
         //t1.Name = "MyNewThread1";
         //t1.IsBackground = true;
         //t1.Start();
         //Console.WriteLine("Main thread ending now...");

         ////Data d = new Data();
         ////d.Message = "Info";
         ////Thread t2 = new Thread(ThreadMainWithParameters);
         ////t2.Start(d);

         //MyThread obj = new MyThread("info");
         //Thread t3 = new Thread(obj.ThreadMain);
         //t3.Start();
      }

      static void Prio()
      {
         for (int i = 0; i < 10000; i++)
         {
            Console.WriteLine("{0}, {1}", Thread.CurrentThread.Name, i);
         }
      }

      static void ThreadMain()
      {
         Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
         Thread.Sleep(3000);
         // Console.WriteLine("Running in the thread {0}, id: {1}.", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
         Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
      }

      static void ThreadMainWithParameters(object o)
      {
         Data d = (Data)o;
         Console.WriteLine("Running in a thread, received {0}", d.Message);
      }
   }
}

⌨️ 快捷键说明

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