sportdog

导航

 

 

  public class ThreadSample
    {

        private readonly int _number;
        public ThreadSample(int number)
        {
            _number = number;
        }

        public void CountNumber()
        {
            for (int i = 0; i < _number; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));
                Console.WriteLine("Current Thread {0} print {1}", Thread.CurrentThread.Name, i);
            }
        }

    }
View Code
 static void Main(string[] args)
        {
            ThreadSample foreGround = new ThreadSample(10);
            ThreadSample backGround = new ThreadSample(30);
            Thread foreThread = new Thread(foreGround.CountNumber);
            foreThread.Name = "ForeThread";
            //foreThread.IsBackground = false;
           
            Thread backThread = new Thread(backGround.CountNumber);
            backThread.Name = "BackThread";
            backThread.IsBackground = true; 
            foreThread.Start();
            backThread.Start();
            Console.ReadLine();  //这行要注释掉,否则后台进程会继续进行
 
        }

 

posted on 2018-08-20 10:44  sportdog  阅读(114)  评论(0编辑  收藏  举报