public partial class IPRSRestart : ServiceBase { System.Timers.Timer _Timer; //计时器 private static object _LockSMS_Send = new object(); public IPRSRestart() { InitializeComponent(); } protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 int minute =1; this._Timer = new System.Timers.Timer(); this._Timer.Interval = minute * 60 * 1000; //设置计时器事件间隔执行时间 this._Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); this._Timer.Enabled = true; } protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 this._Timer.Enabled = false; } private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { RestartService("360DesktopLiteApp");//需要重启的服务名称 } public static void RestartService(string serviceName) { ServiceController service = new ServiceController(serviceName); using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\IPRSLog\\log.txt", true)) { try { if (service != null) { if (service.Status == ServiceControllerStatus.Running) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "已停止."); } service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "已启动."); } } catch(Exception ex) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ex.Message); } } } }
留言评论