程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#多線實例

c#多線實例

編輯:C#入門知識

using System;
using System.Threading;
using System.Text;

namespace ControlThread
{
    class Program
    {
        private static void DisplayMessage()
        {

            // 在控制台上重復顯示一條消息.
            while (true)
            {

                try
                {

                    Console.WriteLine("{0} : 第二個線程正在運行,請輸入"
                        + " (S)uspend, (R)esume, (I)nterrupt, or (E)xit.",
                        DateTime.Now.ToString("HH:mm:ss.ffff"));

                    // 睡眠 2 秒種.
                    Thread.Sleep(2000);

                }
                catch (ThreadInterruptedException)
                {

                    // 線程被中斷. 捕獲到ThreadInterruptedException異常後
                    // 示例會執行合適的行為並且繼續執行.
                    Console.WriteLine("{0} : 第二個線程被中斷.",
                        DateTime.Now.ToString("HH:mm:ss.ffff"));

                }
                catch (ThreadAbortException abortEx)
                {

                    // ThreadAbortException對象.
                    //線程調用Thread.Abort後所提供的ExceptionState屬性
                    //本例中是包含了一個終止原因的字符串
                    Console.WriteLine("{0} : 第二個線程被終止. ({1})",
                        DateTime.Now.ToString("HH:mm:ss.ffff"),
                        abortEx.ExceptionState);

                    // 盡管 ThreadAbortException已經被處理, www.2cto.com
                    //運行時還是會拋出一個異常以確保線程被終止.

                }
            }

        }

        static void Main(string[] args)
        {
            // 創建一個新的線程對象,
            // 傳遞給他一個執行顯示消息方法的ThreadStart委托實列.
          
            Thread thread = new Thread(new ThreadStart(DisplayMessage));

            Console.WriteLine("{0} : 啟動第二個線程.",
                DateTime.Now.ToString("HH:mm:ss.ffff"));

            // 啟動第二個線程.
            thread.Start();

            // 循環處理用戶輸入的命令.
            char command = ' ';

            do
            {

                string input = Console.ReadLine();
                if (input.Length > 0) command = input.ToUpper()[0];
                else command = ' ';

                switch (command)
                {

                    case 'S':
                        // 掛起第二個線程.
                        Console.WriteLine("{0} : 掛起第二個線程.",
                            DateTime.Now.ToString("HH:mm:ss.ffff"));
                        thread.Suspend();
                        break;

                    case 'R':
                        // 繼續第二個線程執行.
                        try
                        {
                            Console.WriteLine("{0} : 繼續第二個線程執行.",
                                DateTime.Now.ToString("HH:mm:ss.ffff"));
                            thread.Resume();
                        }
                        catch (ThreadStateException)
                        {
                            Console.WriteLine("{0} : 線程並沒有被掛起.",
                                DateTime.Now.ToString("HH:mm:ss.ffff"));
                        }
                        break;

                    case 'I':
                        // 中斷第二個線程.
                        Console.WriteLine("{0} : 中斷第二個線程.",
                            DateTime.Now.ToString("HH:mm:ss.ffff"));
                        thread.Interrupt();
                        break;

                    case 'E':
                        // 終止第二個線程,並且為已經中止的線程傳遞一個狀態對象
                        // 本例是一條消息.
                        Console.WriteLine("{0} : 正在終止第二個線程.",
                            DateTime.Now.ToString("HH:mm:ss.ffff"));

                        thread.Abort("終止示例.");

                        // 等待第二個線程結束.
                        thread.Join();
                        break;
                }
            } while (command != 'E');

            // 等待繼續.
            Console.WriteLine("主方法結束. 按下回車鍵.");
            Console.ReadLine();

        }
    }
}

 

摘自 rayhuang110的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved