定时器的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers; // 定时器

namespace Demo4Timers
{
    class Program
    {
        static int counter = 0;
        static string displayString = "Hello C#!";

        static void WriteChar(object source,ElapsedEventArgs e)
        {
            Console.Write(displayString[counter++ % displayString.Length]); // 一个单词一个单词写!
        }

        static void Main(string[] args)
        {
            Timer myTimer = new Timer(100);
            myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
            myTimer.Start();
            System.Threading.Thread.Sleep(200); // 每2毫秒执行一次
            Console.ReadKey();
        }
    }
}

执行效果很有科技感!

posted @ 2017-05-02 19:02  TBHacker  阅读(188)  评论(0)    收藏  举报