运行管理员线程和用户线程小练习

 

今天学习C#中线程的知识点,于是动手编些有关线程的小例子,用来理解线程的运行原理,希望能够帮助大家尽快入手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConTest
{
    class Program
{
        static void Main(string[] args)
        {
            while (true)
            {
                Display();
                RunThread();
                Console.Clear();
            }    
           
         
     
            Console.Read();

        }

        private static void RunThread()
        {
            int input = Convert.ToInt32(Console.ReadLine());
            ThreadSample ts = new ThreadSample();
            switch (input)
            {
                case 1:
                    ts.ThreadStartToWork(ThreadSample.ThreadCatagory.AdminThread);
                    break;
                case 2:
                    ts.ThreadStartToWork(ThreadSample.ThreadCatagory.UserThread);
                    break;
                default:
                    //Thread.CurrentThread.Abort();                  
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                    break;
            }
        }

        private static void Display()
        {
            Console.WriteLine("*******************************************");
            for (int i = 0; i < 7; i++)
            {
                if (i == 3)
                {
                    Console.WriteLine("*         1、管理员线程工作               *");
                    Console.WriteLine("*                                         *");


                }
                else if (i == 4)
                {
                    Console.WriteLine("*         2、普通用户线程工作             *");
                    Console.WriteLine("*                                         *");
                    Console.WriteLine("*         3、输入0退出程序                *");
                    Console.WriteLine("*                                         *");


                }
                else
                {
                    Console.WriteLine("*                                         *");
                }

            }
            Console.WriteLine("*******************************************");
            Console.WriteLine("请选择你需要的工作线程:");
        }

        static void ShowMsg()
        {
        }
          

       

    }
    class ThreadSample
    {
        public enum ThreadCatagory
        {
            AdminThread,
            UserThread
        }

        ThreadStart s;
        Thread t;
       
        public void ThreadStartToWork(ThreadCatagory start)
        {

            switch (start)
            {
                   
                case ThreadCatagory.AdminThread:
                    s = new ThreadStart(AdminThread);
                    break;
                case ThreadCatagory.UserThread:
                    s = new ThreadStart(UserThread);
                    break;
                default:
                    break;
            }
           t = new Thread(s);
         
           t.Start();
            t.Join();//阻塞调用线程,直到当前线程执行完毕,再执行调用线程(这是关键点否则出现不合逻辑的结果)
        }

        public void AdminThread()
        {
            Console.WriteLine("管理员线程正在启动......");
            Thread.Sleep(5000);
            Console.WriteLine("管理员线程正在运行中,请顾客耐心等等!!!!");
            Thread.Sleep(5000);

            Console.WriteLine("管理员线程正在结束,倒计时开始...");
            for (int i = 5; i >=1; i--)
   {
   
                Thread.Sleep(1000);
                Console.WriteLine(i);
   }
            Console.WriteLine("管理员线程已经结束了");        
            Thread.CurrentThread.Abort();
        }

        public void UserThread()
        {

            Console.WriteLine("用户线程正在启动......");
            Thread.Sleep(5000);
            Console.WriteLine("用户线程正在运行中,请顾客耐心等等!!!!");
            Thread.Sleep(5000);

            Console.WriteLine("用户线程正在结束,倒计时开始...");
            for (int i = 5; i >= 1; i--)
            {

                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
            Console.WriteLine("用户线程已经结束了");        
            Thread.CurrentThread.Abort();
        }
    }
}

posted @ 2013-03-22 15:59  Predator  阅读(141)  评论(0编辑  收藏  举报