定时启动Windows服务

来源:互联网 发布:linkdeque java 编辑:程序博客网 时间:2024/06/06 06:52
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.IO;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;namespace 定时启动服务{    public partial class Service1 : ServiceBase    {        System.Timers.Timer t = new System.Timers.Timer(5000);  //设置时间间隔为5秒        public Service1()        {            InitializeComponent();            t.Elapsed += new System.Timers.ElapsedEventHandler(chksrv);  //达到间隔的时间是执行的事件        }        protected override void OnStart(string[] args)        {            t.Enabled = true; //是否触发Elapsed事件            t.Start();        }        public void chksrv(object source,System.Timers.ElapsedEventArgs e)        {                    string dt = DateTime.Now.ToString();                    string a = e.ToString();                    FileStream fs = new FileStream(@"D:\Error.txt", FileMode.Append);                    StreamWriter sw = new StreamWriter(fs);                    sw.WriteLine(dt);                    sw.WriteLine(a);                    sw.Close();                    fs.Close();        }            protected override void OnStop()        {        }    }}

0 0
原创粉丝点击