原来是发在优快云论坛的,索性拿出来给更多的人分享 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Net; using System.Net.Sockets; namespace wsPing { public class pingService : System.ServiceProcess.ServiceBase { public System.Diagnostics.EventLog evLog; private System.Timers.Timer TimerPing; /** <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; //ping 一个页面地址********************************* int iPingInterval =180000;//3分钟 string sPingAddress="www.buynow.com.cn"; //************************************************** public pingService() { // 该调用是 Windows.Forms 组件设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 //如果不存在日志************************************ if(!System.Diagnostics.EventLog.SourceExists("logService")) { EventLog.CreateEventSource("logService","logServiceLog"); } this.evLog.Source="logService"; //如果要重新命名,必须重新启动计算机 //this.evLog.Log="logServiceLog"; //************************************************** } // 进程的主入口点 static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // 同一进程中可以运行多个用户服务。若要将 //另一个服务添加到此进程,请更改下行 // 以创建另一个服务对象。例如, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new pingService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /** <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this.evLog = new System.Diagnostics.EventLog(); this.TimerPing = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.evLog)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.TimerPing)).BeginInit(); // // TimerPing // this.TimerPing.Interval = 60000; this.TimerPing.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerPing_Elapsed); // // pingService // this.CanHandlePowerEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.ServiceName = "pingService"; ((System.ComponentModel.ISupportInitialize)(this.evLog)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.TimerPing)).EndInit(); } /** <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /** <summary> /// 设置具体的操作,以便服务可以执行它的工作。 /// </summary> protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 this.evLog.WriteEntry("pingService is Starting……………………………"); TimerPing.Interval=this.iPingInterval; this.TimerPing.Enabled=true; } /** <summary> /// 停止此服务。 /// </summary> protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 this.evLog.WriteEntry("pingService is Stopping……………………………"); this.TimerPing.Enabled=false; } /** <summary> /// 暂停 /// </summary> protected override void OnPause() { this.evLog.WriteEntry("pingService is Pausing……………………………"); } /** <summary> ///继续 /// </summary> protected override void OnContinue() { this.evLog.WriteEntry("pingService is Continuing……………………………"); } private void TimerPing_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Pinger pi=new Pinger(); if(pi.Ping(this.sPingAddress)<1) { evLog.WriteEntry(sPingAddress +" does not respond."); } } } public class Pinger { public int Ping (string addr) { Socket sck=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); try { System.Net.IPHostEntry ipInfo=System.Net.Dns.Resolve(addr); IPEndPoint ipe=new IPEndPoint(ipInfo.AddressList[0],8); sck.Connect(ipe); } catch { return -1; } return 1; } } } using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Net; using System.Net.Sockets; namespace wsPing { public class pingService : System.ServiceProcess.ServiceBase { public System.Diagnostics.EventLog evLog; private System.Timers.Timer TimerPing; /**/ /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null ; // ping 一个页面地址********************************* int iPingInterval = 180000 ; // 3分钟 string sPingAddress = " www.buynow.com.cn " ; // ************************************************** public pingService() { // 该调用是 Windows.Forms 组件设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 // 如果不存在日志************************************ if ( ! System.Diagnostics.EventLog.SourceExists( " logService " )) { EventLog.CreateEventSource( " logService " , " logServiceLog " ); } this .evLog.Source = " logService " ; // 如果要重新命名,必须重新启动计算机 // this.evLog.Log="logServiceLog"; // ************************************************** } // 进程的主入口点 static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // 同一进程中可以运行多个用户服务。若要将 // 另一个服务添加到此进程,请更改下行 // 以创建另一个服务对象。例如, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new pingService() } ; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /**/ /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this .evLog = new System.Diagnostics.EventLog(); this .TimerPing = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)( this .evLog)).BeginInit(); ((System.ComponentModel.ISupportInitialize)( this .TimerPing)).BeginInit(); // // TimerPing // this .TimerPing.Interval = 60000 ; this .TimerPing.Elapsed += new System.Timers.ElapsedEventHandler( this .TimerPing_Elapsed); // // pingService // this .CanHandlePowerEvent = true ; this .CanPauseAndContinue = true ; this .CanShutdown = true ; this .ServiceName = " pingService " ; ((System.ComponentModel.ISupportInitialize)( this .evLog)).EndInit(); ((System.ComponentModel.ISupportInitialize)( this .TimerPing)).EndInit(); } /**/ /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if ( disposing ) { if (components != null ) { components.Dispose(); } } base .Dispose( disposing ); } /**/ /// <summary> /// 设置具体的操作,以便服务可以执行它的工作。 /// </summary> protected override void OnStart( string [] args) { // TODO: 在此处添加代码以启动服务。 this .evLog.WriteEntry( " pingService is Starting…………………………… " ); TimerPing.Interval = this .iPingInterval; this .TimerPing.Enabled = true ; } /**/ /// <summary> /// 停止此服务。 /// </summary> protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 this .evLog.WriteEntry( " pingService is Stopping…………………………… " ); this .TimerPing.Enabled = false ; } /**/ /// <summary> /// 暂停 /// </summary> protected override void OnPause() { this .evLog.WriteEntry( " pingService is Pausing…………………………… " ); } /**/ /// <summary> /// 继续 /// </summary> protected override void OnContinue() { this .evLog.WriteEntry( " pingService is Continuing…………………………… " ); } private void TimerPing_Elapsed( object sender, System.Timers.ElapsedEventArgs e) { Pinger pi = new Pinger(); if (pi.Ping( this .sPingAddress) < 1 ) { evLog.WriteEntry(sPingAddress + " does not respond. " ); } } } public class Pinger { public int Ping ( string addr) { Socket sck = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); try { System.Net.IPHostEntry ipInfo = System.Net.Dns.Resolve(addr); IPEndPoint ipe = new IPEndPoint(ipInfo.AddressList[ 0 ], 8 ); sck.Connect(ipe); } catch { return - 1 ; } return 1 ; } } }