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

📄 ex-20-02

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 20-02: Interrupting a thread

namespace Programming_CSharp
{
   using System;
   using System.Threading;

   class Tester
   {
      static void Main()
      {
         // make an instance of this class
         Tester t = new Tester();

         // run outside static Main
         t.DoTest();                 
      }

      public void DoTest()
      {
         // create an array of unnamed threads
         Thread[] myThreads = 
            {
               new Thread( new ThreadStart(Decrementer) ),
               new Thread( new ThreadStart(Incrementer) ),
               new Thread( new ThreadStart(Incrementer) )              
            };

         // start each thread
         int ctr = 1;
         foreach (Thread myThread in myThreads)
         {
            myThread.IsBackground=true;
            myThread.Start();
            myThread.Name = "Thread" + ctr.ToString();
            ctr++;
            Console.WriteLine("Started thread {0}", myThread.Name);
            Thread.Sleep(50);
         }

         // having started the threads
         // tell thread 1 to kill itself
         myThreads[1].Interrupt();

         // wait for all threads to end before continuing
         foreach (Thread myThread in myThreads)
         {
            myThread.Join();
         }

         // after all threads end, print a message
         Console.WriteLine("All my threads are done.");
      }

      // demo function, counts down from 1k
      public void Decrementer()
      {
         try
         {
            for (int i = 1000;i>=0;i--)
            {
               Console.WriteLine(
                  "Thread {0}. Decrementer: {1}", 
                  Thread.CurrentThread.Name,
                  i);
               Thread.Sleep(1);
            }
         }
         catch (ThreadInterruptedException)
         {
            Console.WriteLine(
               "Thread {0} interrupted! Cleaning up...",
               Thread.CurrentThread.Name);
         }
         finally
         {
            Console.WriteLine(
               "Thread {0} Exiting. ",
               Thread.CurrentThread.Name);
         }
      }

      // demo function, counts up to 1K
      public void Incrementer()
      {
         try
         {
            for (int i =0;i<1000;i++)
            {
               Console.WriteLine(
                  "Thread {0}. Incrementer: {1}", 
                  Thread.CurrentThread.Name,
                  i);
               Thread.Sleep(1);
            }
         }
         catch (ThreadInterruptedException)
         {
            Console.WriteLine(
               "Thread {0} interrupted! Cleaning up...",
               Thread.CurrentThread.Name);
         }
         finally
         {
            Console.WriteLine(
               "Thread {0} Exiting. ",
               Thread.CurrentThread.Name);
         }
      }
   }
}

⌨️ 快捷键说明

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