日期:2014-05-17  浏览次数:20431 次

C#windows服务计时器问题


  public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;  //计时器
        string message = "Test Message 调用记录!";
        public Service1()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            //DateTime dtNow = DateTime.Now;
            //if (dtNow.Hour < 24)
            //{
            //    int hour = 24 - dtNow.Hour;
            //    int minite = 60 - dtNow.Minute;
            //    int miao = 60 - dtNow.Second;
            //    int sleeptime = (hour * 60 * 60 * 1000) + (minite * 60 * 1000) + (miao * 1000);
            //}

            timer1 = new System.Timers.Timer();
            timer1.Interval = 24 * 60 * 60 * 1000;  //设置计时器事件间隔执行时间
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            timer1.Enabled = true;
        }
        protected override void OnStop()
        {
            this.timer1.Enabled = false;
        }
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try {
                //执行通知运营       
            }
            catch (Exception ex)
            {
                message = ex.Message;
                File.WriteAllText(@"D:\SubOr