2011-06-15 93 views
13

尝试启动Windows服务时,某些服务会自动停止,如果它们未被其他服务使用,则会自动停止。某些服务如果未被其他服务使用,将自动停止

我不使用Windows服务配置文件,并使用静态属性服务 - 它工作正常

现在,我使用app.config文件,重建我的设置项目+服务项目。现在我安装服务,然后尝试启动该服务 - 我得到以下错误:

一些服务自动停止,如果他们不使用其他服务

服务作为本地系统登录。

欢迎任何输入!谢谢。

回答

29

这通常是两件事之一的结果 - 要么(a)您的OnStart()方法抛出异常或(b)OnStart()方法没有开始执行工作的线程。

如果问题是(a),那么显而易见的解决方案是调试服务以确定哪里出了问题。至少,在OnStart()方法的内容周围放置一个try-catch块,并在发生异常时将错误记录到系统事件日志中。然后,您可以在Windows事件查看器中查看详细信息。

如果问题是(b),那么您需要创建一个实际执行某个操作的线程。该线程需要是前台线程(而不是后台线程)以防止服务关闭。一个典型的OnStart()方法是这样的:

private System.Threading.Thread _thread; 

protected override void OnStart(string[] args) 
{ 
    try 
    { 
     // Uncomment this line to debug... 
     //System.Diagnostics.Debugger.Break(); 

     // Create the thread object that will do the service's work. 
     _thread = new System.Threading.Thread(DoWork); 

     // Start the thread. 
     _thread.Start(); 

     // Log an event to indicate successful start. 
     EventLog.WriteEntry("Successful start.", EventLogEntryType.Information); 
    } 
    catch (Exception ex) 
    { 
     // Log the exception. 
     EventLog.WriteEntry(ex.Message, EventLogEntryType.Error); 
    } 
} 

private void DoWork() 
{ 
    // Do the service work here... 
} 
+0

我也得到同样的错误,并从这个答案得到解决方案... 谢谢。 – 2013-11-18 10:52:45

+0

是的,我发现到Windows事件日志的服务错误。 – asraful009 2015-02-12 05:33:15

+0

更新答案以及兄弟。更正EventLog的拼写.... 有用的答案,谢谢@MattDavis – 2015-05-12 07:37:13

2

我得到这个错误,这是因为硬盘已经填满。它可能是任何使服务无法运行的东西。