window服務

 partial class MainService : ServiceBase
    {
        public MainService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此處添加代碼以啓動服務。

            //寫日誌啓動服務
            this.timer1.Enabled = true;
           
        }

        protected override void OnStop()
        {
            // TODO: 在此處添加代碼以執行停止服務所需的關閉操作。
            this.timer1.Enabled = false;
        }


        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this.timer1.Enabled = false;

                this.timer1.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["CheckTime"].ToString());
                WarningClass wClass = new WarningClass();
                wClass.Sync();

            }
            catch (Exception ex)
            {
                //寫日誌
            }
            finally
            {
                this.timer1.Enabled = true;
            }
        }


    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;

namespace SyncSmsService
{
    partial class MainService : ServiceBase
    {
        public MainService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此處添加代碼以啓動服務。
            //寫日誌啓動服務
            this.timer1.Enabled = true;
        }

        protected override void OnStop()
        {
            // TODO: 在此處添加代碼以執行停止服務所需的關閉操作。
            this.timer1.Enabled = false;
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this.timer1.Enabled = false;

                this.timer1.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["CheckTime"].ToString());
                SmsDAL dal = new SmsDAL();
                dal.SendSms();

            }
            catch (Exception ex)
            {
                //寫日誌
            }
            finally
            {
                this.timer1.Enabled = true;
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章