日期:2014-05-17 浏览次数:20578 次
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.IO;
namespace WSForXMLReport
{
    public partial class ServiceXML : ServiceBase
    {
        System.Timers.Timer timer = null;
        public ServiceXML()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 1000 * 5;  //5s
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Start();
        }
        protected override void OnStop()
        {
            timer.Enabled = false;
            //页面中使用:System.Web.HttpContext.Current.Server.MapPath
            string strPath = AppDomain.CurrentDomain.BaseDirectory;
            string path = strPath + @"/ErrStop.txt";
            //文件不存在就创建然后读写该文件,会提示文件被占用。
            if (!File.Exists(path))
            {
                FileStream fs = File.Create(path);//若文件不存在就先创建 
                fs.Close();
            }
            StreamWriter sw = new StreamWriter(path, true);
            sw.WriteLine(DateTime.Now + ":stop ok");
            sw.Flush();
            sw.Close();
        }
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer.Enabled = false;
            #region 定时执行程序
            //在这里编写需要定时执行的逻辑代码
            //FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();
            //List<FFWS.Model.earlyWarningInfo> lst = eBll.GetStcdRealDataReport();
            string str = "abc";
            //FFWS.Web.WebReference.IReportReceiveService service = new FFWS.Web.WebReference.IReportReceiveService();
            //Global文件只能使用下面的方法查找目录,可能是应用程序还未启动。
            //页面中使用:System.Web.HttpContext.Current.Server.MapPath
            string strPath = AppDomain.CurrentDomain.BaseDirectory;
            string path = strPath + @"/Err.txt";
            //文件不存在就创建然后读写该文件,会提示文件被占用。
            if (!File.Exists(path))
            {
                FileStream fs = File.Create(path);//若文件不存在就先创建 
                fs.Close();
            }
            StreamWriter sw = new StreamWriter(path, true);
            sw.WriteLine(DateTime.Now + " " + str + ": start");
            sw.Flush();
            sw.Close();
            #endregion
            this.timer.Enabled = true;
        }
    }
}