使用Timer

最近总有朋友遇到Timer的使用问题,比如如何实现TimerCallBack,还有就是稳定性问题,Timer莫名其妙就不OnTimer了。

 

其实实现很简单,一句话就行了(高亮的那句),另外对于Win7、Win 08 Server可以设置.exe的兼容性为Visata或win 03,这样不容易引起休眠,尽可能减少Timer不OnTimer的情况。

using System;
using System.Linq;
using System.Threading;
using System.IO;
namespace ClientConsole
{
    class Program
    {
        const string FileName = "log.txt";
        static Timer timer = new Timer((x) => File.AppendAllText(FileName, DateTime.Now.ToLongTimeString() + Environment.NewLine), null, 0, 3000);
        static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }
}
 

 

 

posted @ 2011-04-12 14:00  蜡笔小王  阅读(480)  评论(0编辑  收藏  举报