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()
{
}
}
}
定时启动Windows服务
最新推荐文章于 2024-01-29 09:03:09 发布