1. 程式人生 > >一小時看一次,每隔一天執行一次

一小時看一次,每隔一天執行一次

_timer = new Timer();
            _timer.Interval = 60 * 60 * 1000; //一個小時
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Enabled = true;
        }

        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (DateTime.Now < _nextExecuteTime)
                return;
            try
            {
                Run();
                _nextExecuteTime = DateTime.Now.AddDays(1).Date;
            }
            catch (Exception ex)
            {
                Logger.ErrorWithFormat("Failed to start timer. ex:{0}. ", ex.Message + ex.StackTrace);
                _nextExecuteTime = DateTime.Now.AddHours(1);
            }
        }