///< summary> private void tmrWinService_Elapsed(Object sender, ///< summary> private void tmrWinService_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) { this.tmrWinService.Stop(); 尝试 { UserServices oUser = new UserServices(); ScriptingTelnet st = new ScriptingTelnet(); // CALL oUser.processIAR(); } catch(例外错误) { //查看日志是否存在。若否,则创建 if(!EventLog.SourceExists(" IAR-Service")) EventLog.CreateEventSource(""," IAR-Service","。"); // log EventLog log = new EventLog(); log.Log =" IAR-Service"; log.Source =" WinServiceIAR.tmrWinService_Elapsed"; log.WriteEntry(err.Message.ToString(),EventLogEntryType.Error); log.Dispose(); } 最后 {这个.tmrWinService.Start(); } } } Willy。 " Jacob Crossley" < JA *********** @ hotmail.com>在消息中写道 新闻:il *************** @ fe25.usenetserver.com ... 这只是实例化另一个sln中的一个类......它是一个自定义类,它可以在路由器上添加和删除人员以限制/允许在我们的ISP上访问互联网...我可以包括那个代码,如果你愿意,但所有我们的服务都会挂起,不管他们在工人班里做什么......我会建议服务挂起即使它把一个写成hello world的类称为文本文件。 无需用简单的hello来检查世界"服务:-),我有一个 的服务,现在运行几周,使用System.Threading.Timer以常规 的间隔执行一些任务。 这个自定义类可能需要比间隔更长的时间吗?并且, 当它存在的可能性时,你确定,你的自定义类是 线程安全,并且没有可能导致竞争条件 死锁? 我建议您观看一些资源,如句柄数量,可运行线程数量 服务运行时以及何时挂起。 你可以做的另一件事是将一个调试器附加到你挂起的服务上并尝试 找出它挂起的位置。 威利。 Hello all. We have about 10 Window''s services that we wrote in c#. We usethem to process row''s that we have queued up in various SQL tables. The services seem to hang at least once in any given 24 hour period. Once wereset the service, it goes about its processing business until the next timeit hangs. We are wondering if the hanging problem is due to a load issue(too many windows services on a single machine), a code flaw, or an inherentc# bug? We used to write NT services in VB6 using the ntsvc.ocx. Thoseservices never hung. I''m wondering if we are going to have to rewrite all ofour c# services in old school vb6. I''ve included the Service codebelow...any help would be much appreciated. using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Configuration; namespace IAR_Service{public class IAR : System.ServiceProcess.ServiceBase{private System.Timers.Timer tmrWinService;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null; public IAR(){// This call is required by the Windows.Forms Component Designer.InitializeComponent(); // TODO: Add any initialization after the InitComponent call} // The main entry point for the processstatic void Main(){System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add// another service to this process, change the following line to// create a second service object. For example,//// ServicesToRun = New System.ServiceProcess.ServiceBase[] {newService1(), new MySecondUserService()};//ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() }; System.ServiceProcess.ServiceBase.Run(ServicesToRu n);} /// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.tmrWinService = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit();//// tmrWinService////this.tmrWinService.Enabled = true;this.tmrWinService.Interval = 20000;this.tmrWinService.Elapsed += newSystem.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);//// IAR//this.AutoLog = false;this.CanPauseAndContinue = true;this.ServiceName = "IAR"; ((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit(); } /// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null){components.Dispose();}}base.Dispose( disposing );} /// <summary>/// Set things in motion so your service can do its work./// </summary>protected override void OnStart(string[] args){// TODO: Add code here to start your service.tmrWinService.Enabled = true;tmrWinService.AutoReset = true;tmrWinService.Start();//see if log exists. if not, createif (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("","IAR-Service", ".");//logEventLog log = new EventLog();log.Log = "IAR-Service";log.Source = "WinServiceIAR.OnStart";log.WriteEntry("Starting IAR windows Service",EventLogEntryType.Information);log.Dispose();} /// <summary>/// Stop this service./// </summary>protected override void OnStop(){// TODO: Add code here to perform any tear-down necessary to stop yourservice.tmrWinService.Enabled = true;tmrWinService.AutoReset = true;tmrWinService.Stop();//see if log exists. if not, createif (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("","IAR-Service", ".");//logEventLog log = new EventLog();log.Log = "IAR-Service";log.Source = "WinServiceIAR.OnStop";log.WriteEntry("Stopping IAR windows Service",EventLogEntryType.Information);log.Dispose();} private void tmrWinService_Elapsed(Object sender,System.Timers.ElapsedEventArgs e){this.tmrWinService.Stop();try{UserServices oUser = new UserServices();ScriptingTelnet st = new ScriptingTelnet();//CALLoUser.processIAR(); }catch (Exception err){//see if log exists. if not, createif (!EventLog.SourceExists("IAR-Service"))EventLog.CreateEventSource("", "IAR-Service", ".");//logEventLog log = new EventLog();log.Log = "IAR-Service";log.Source = "WinServiceIAR.tmrWinService_Elapsed";log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error);log.Dispose();}finally{this.tmrWinService.Start();}}}} 解决方案 What''s the purpose of this? ScriptingTelnet st = new ScriptingTelnet(); Willy. "Jacob Crossley" <ja***********@hotmail.com> wrote in messagenews:WY***************@fe25.usenetserver.com... Hello all. We have about 10 Window''s services that we wrote in c#. We use them to process row''s that we have queued up in various SQL tables. The services seem to hang at least once in any given 24 hour period. Once we reset the service, it goes about its processing business until the next time it hangs. We are wondering if the hanging problem is due to a load issue (too many windows services on a single machine), a code flaw, or an inherent c# bug? We used to write NT services in VB6 using the ntsvc.ocx. Those services never hung. I''m wondering if we are going to have to rewrite all of our c# services in old school vb6. I''ve included the Service code below...any help would be much appreciated. using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Configuration; namespace IAR_Service { public class IAR : System.ServiceProcess.ServiceBase { private System.Timers.Timer tmrWinService; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public IAR() { // This call is required by the Windows.Forms Component Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call } // The main entry point for the process static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() }; System.ServiceProcess.ServiceBase.Run(ServicesToRu n); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tmrWinService = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit() ; // // tmrWinService // //this.tmrWinService.Enabled = true; this.tmrWinService.Interval = 20000; this.tmrWinService.Elapsed += new System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed); // // IAR // this.AutoLog = false; this.CanPauseAndContinue = true; this.ServiceName = "IAR"; ((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// <summary> /// Set things in motion so your service can do its work. /// </summary> protected override void OnStart(string[] args) { // TODO: Add code here to start your service. tmrWinService.Enabled = true; tmrWinService.AutoReset = true; tmrWinService.Start(); //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.OnStart"; log.WriteEntry("Starting IAR windows Service", EventLogEntryType.Information); log.Dispose(); } /// <summary> /// Stop this service. /// </summary> protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. tmrWinService.Enabled = true; tmrWinService.AutoReset = true; tmrWinService.Stop(); //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.OnStop"; log.WriteEntry("Stopping IAR windows Service", EventLogEntryType.Information); log.Dispose(); } private void tmrWinService_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) { this.tmrWinService.Stop(); try { UserServices oUser = new UserServices(); ScriptingTelnet st = new ScriptingTelnet(); //CALL oUser.processIAR(); } catch (Exception err) { //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.tmrWinService_Elapsed"; log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error); log.Dispose(); } finally { this.tmrWinService.Start(); } } } }That just instantiates another class in the sln....Its a custom class thatadds and removes people from an acl on a router to restrict/permit internetaccess at our ISP...I could include that code if you''d like, but all of ourservices hang, regardless of what they do in the worker classes...I''dpropose that the service would hang even if it called a class that wrote''hello world'' to a text file."Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in messagenews:uv**************@TK2MSFTNGP09.phx.gbl... What''s the purpose of this? ScriptingTelnet st = new ScriptingTelnet(); Willy. "Jacob Crossley" <ja***********@hotmail.com> wrote in message news:WY***************@fe25.usenetserver.com... Hello all. We have about 10 Window''s services that we wrote in c#. Weuse them to process row''s that we have queued up in various SQL tables. The services seem to hang at least once in any given 24 hour period.Once we reset the service, it goes about its processing business until the next time it hangs. We are wondering if the hanging problem is due to a load issue (too many windows services on a single machine), a code flaw, or an inherent c# bug? We used to write NT services in VB6 using the ntsvc.ocx. Those services never hung. I''m wondering if we are going to have to rewriteall of our c# services in old school vb6. I''ve included the Service code below...any help would be much appreciated. using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Configuration; namespace IAR_Service { public class IAR : System.ServiceProcess.ServiceBase { private System.Timers.Timer tmrWinService; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public IAR() { // This call is required by the Windows.Forms Component Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call } // The main entry point for the process static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() }; System.ServiceProcess.ServiceBase.Run(ServicesToRu n); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tmrWinService = new System.Timers.Timer();((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit() ; // // tmrWinService // //this.tmrWinService.Enabled = true; this.tmrWinService.Interval = 20000; this.tmrWinService.Elapsed += new System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed); // // IAR // this.AutoLog = false; this.CanPauseAndContinue = true; this.ServiceName = "IAR";((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// <summary> /// Set things in motion so your service can do its work. /// </summary> protected override void OnStart(string[] args) { // TODO: Add code here to start your service. tmrWinService.Enabled = true; tmrWinService.AutoReset = true; tmrWinService.Start(); //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.OnStart"; log.WriteEntry("Starting IAR windows Service", EventLogEntryType.Information); log.Dispose(); } /// <summary> /// Stop this service. /// </summary> protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. tmrWinService.Enabled = true; tmrWinService.AutoReset = true; tmrWinService.Stop(); //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.OnStop"; log.WriteEntry("Stopping IAR windows Service", EventLogEntryType.Information); log.Dispose(); } private void tmrWinService_Elapsed(Object sender, System.Timers.ElapsedEventArgs e) { this.tmrWinService.Stop(); try { UserServices oUser = new UserServices(); ScriptingTelnet st = new ScriptingTelnet(); //CALL oUser.processIAR(); } catch (Exception err) { //see if log exists. if not, create if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("", "IAR-Service", "."); //log EventLog log = new EventLog(); log.Log = "IAR-Service"; log.Source = "WinServiceIAR.tmrWinService_Elapsed"; log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error); log.Dispose(); } finally { this.tmrWinService.Start(); } } } }Willy. "Jacob Crossley" <ja***********@hotmail.com> wrote in messagenews:il***************@fe25.usenetserver.com... That just instantiates another class in the sln....Its a custom class that adds and removes people from an acl on a router to restrict/permit internet access at our ISP...I could include that code if you''d like, but all of our services hang, regardless of what they do in the worker classes...I''d propose that the service would hang even if it called a class that wrote ''hello world'' to a text file. No need to check this with a simple "hello world" service :-),I have acouple of services ,running for weeks now, performing some tasks at regularintervals using System.Threading.Timer. Is it possible that this custom class takes longer than the interval? And,when it''s the possibility exists, are you sure that, your custom class isthreadsafe, and there is no possibility for race conditions leading todeadlocks?I suggest you watch some resources like # of handles, # of run-able threadswhen the service runs and when it hangs.Another thing you can do is attach a debugger to your hung service and tryto find out where it hangs. Willy. 这篇关于Windows服务挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 10:21