1、在.net中,windows服务的实现类必需继承于System.ServiceProcess.ServiceBase public partial class myService : ServiceBase { } 2、在windows服务的实现类的构造函数中进行必要的初始化工作,如设置系统标识服务的简短名称等。 public partial class myService : ServiceBase { public SqlBackupService() { ServiceName = "Myservice"; AutoLog = false; CanStop = true; } } 3、重写OnStar和OnStop函数,在OnStar里实现具体功能,OnStop中释放OnStat中创建的资源。 public partial class myService : ServiceBase { private int tickcount = 0; private System.Timers.Timer t = null; public SqlBackupService() { ServiceName = "Myservice"; AutoLog = false; CanStop = true; } protected override void OnStart(string[] args) { Console.WriteLine("Myservice start …"); //建立定时器 t = new System.Timers.Timer(10000); t.AutoReset = true; //每隔10000毫秒触发一次 t.Elapsed += new System.Timers.ElapsedEventHandler(myWork); t.Start(); […]
View Details