window服务

来源:互联网 发布:环球易购海外网络推广 编辑:程序博客网 时间:2024/04/29 17:38

 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;
            }
        }
    }
}

 

原创粉丝点击