C#控制台源程序分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;                        //需要用到线程
namespace timer016
{
    class Program
    {
        static void Main(string[] args)
        {
            StopWatch sw = new StopWatch();
            sw.Start();
        }
    }

    class StopWatch
    {
        private int Interval = 1000;               //时间间隔,单位毫秒
        private int Time = 0;                       //所显示的时间

        public void Start()
        {
            Thread timer = new Thread(new ThreadStart(Timer)); //新建一个线程,该线程调用Timer()
            timer.Start();                              //启动线程
            Console.CursorVisible = false;   //隐藏光标
            Console.ReadKey(true);           //等待按任意键退出
            timer.Abort();                              //终止线程,用于停止秒表
        }

        private void Timer()
        {
            while (true)
            {
                Display();                               //显示秒表计数
                Thread.Sleep(Interval);          //等待1秒后再执行Timer()刷新计数
                Time++;                                 //秒数加1
            }
        }

        private void Display()
        {
            Console.SetCursorPosition(0, 0);
            Console.Write("Time:" + Time.ToString());
        }
    }
}

原文地址:https://www.cnblogs.com/yuerdongni/p/2197064.html