C# Windows 服务自动停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/538925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows service stops automatically
提问by netseng
I made a Window service and let it work automatically and under localsystem account, when the service starts it fires this message for me and then stops
我做了一个 Window 服务,让它在本地系统帐户下自动工作,当服务启动时,它会为我触发这条消息,然后停止
The [service name] service on local computer started and then stopped. Some Services stop automatically if they are not in use by another services or programs.
本地计算机上的 [服务名称] 服务启动然后停止。如果其他服务或程序未使用某些服务,则它们会自动停止。
What's the problem and what's the solution?
有什么问题,有什么解决办法?
采纳答案by Robert Wagner
Either you are not starting any threads on the OnStart method to do work, or there is an exception raised within your OnStart method.
要么您没有在 OnStart 方法上启动任何线程来执行工作,要么在您的 OnStart 方法中引发异常。
If an exception is thrown, it will appear in the Windows Event log. The Windows Event log is a good place to start in any case.
如果抛出异常,它将出现在 Windows 事件日志中。在任何情况下,Windows 事件日志都是一个很好的起点。
Generally an OnStart method looks like this:
通常 OnStart 方法如下所示:
Thread _thread;
protected override void OnStart(string[] args)
{
// Comment in to debug
// Debugger.Break()
// Do initial setup and initialization
Setup();
// Kick off a thread to do work
_thread = new Thread(new MyClass().MyMethod)
_thread.Start();
// Exit this method to indicate the service has started
}
回答by Mat
This particular error message means what it says - that your service has startedbut then quite soon it exitedfor some reason. The good news is that your service is actually doing something, so you have the executable configured and running as a service properly.
这个特定的错误消息意味着它所说的 - 您的服务已启动,但很快由于某种原因退出。好消息是您的服务实际上正在执行某些操作,因此您已将可执行文件配置为作为服务正确运行。
Once started, for some reason it is quitting. You need to find out why this is. Add some debugging to tell you its up and running and known exit cases. If that doesn't reveal the problem then add some debugging to let you know it's still running and work backwards from when that stops.
一旦开始,由于某种原因它正在退出。你需要找出这是为什么。添加一些调试来告诉您它的启动和运行以及已知的退出情况。如果这没有揭示问题,那么添加一些调试,让您知道它仍在运行并从停止时向后工作。
回答by Mike_G
Are you tracing out any debug information? Most likely an exception is being thrown during your initialization. I would trace out all your exceptions and use Debugview to view them.
您是否正在追踪任何调试信息?很可能在初始化期间抛出异常。我会找出您的所有异常并使用 Debugview 查看它们。
回答by Ken Goodridge
I had a similar problem that occurred because my Event Logs were fulland the service was unable to write to them. As such, it was impossible to debug by looking for messages in the Event Viewer. I put a try/catch and dumped the exception out to a file. I had to change the settings on my logs to fill as needed instead of every 7 days and this allowed the services to start.
我遇到了类似的问题,因为我的事件日志已满并且服务无法写入它们。因此,无法通过在事件查看器中查找消息来进行调试。我放了一个 try/catch 并将异常转储到一个文件中。我不得不更改日志上的设置以根据需要填充,而不是每 7 天一次,这样才能启动服务。
Of course, the root of the problem for me is that I have a nVidia driver issue that is flooding my event logs and now I'm probably beating on the disk, but that's another issue.
当然,对我来说问题的根源是我有一个 nVidia 驱动程序问题,它充斥着我的事件日志,现在我可能在磁盘上跳动,但这是另一个问题。
回答by Ullas
For me the same issue came because of a wrong setting in my app.config
file. I changed the setting and the issue resolved
对我来说,由于我的app.config
文件中的设置错误,出现了同样的问题。我更改了设置并解决了问题
回答by Buminda
I had the same issue starting JBoss, then I changed the JAVA_HOME
variable, it worked for me. It was the JBoss version that doesn't support the 1.6, it supports 1.5.
我在启动 JBoss 时遇到了同样的问题,然后我更改了JAVA_HOME
变量,它对我有用。JBoss 版本不支持 1.6,它支持 1.5。
回答by suresh
Maybe you need to run the service as Local System Account. See this post by Srinivas Ganaparthi.
也许您需要以Local System Account 的身份运行该服务。请参阅Srinivas Ganaparthi 的这篇文章。
回答by RRM
I had similar problem and it turned out in my case that the program simply crashed in OnStart method. It tried to read some file that it couldn't find but I suppose that any other program crash would give the same result. In case of Windows forms application you would get some error message but here it was just "your service started and stopped"
我遇到了类似的问题,结果在我的情况下,程序只是在 OnStart 方法中崩溃了。它试图读取一些找不到的文件,但我想任何其他程序崩溃都会给出相同的结果。对于 Windows 窗体应用程序,您会收到一些错误消息,但这里只是“您的服务已启动和停止”
If you ever need, like me to read some files from the directory where Windows Service .exe is located, check this topic: Getting full path for Windows Service
如果您需要像我一样从 Windows Service .exe 所在目录中读取一些文件,请查看此主题: 获取 Windows 服务的完整路径
回答by umesh shukla
In my case, a method in my service, was being called recursively (as no terminate condition being true) and after specific time my service was being stopped.
在我的情况下,我的服务中的一个方法被递归调用(因为没有终止条件为真)并且在特定时间之后我的服务被停止。