该服务无法启动,但确实启动了吗?

Flo*_*ndt 6 c# service command-line windows-services

我写了一个小的Windows服务,我想在我的Windows Server 2008上每天运行.该服务是用C#编写的.

  • 该代码在正常的Windows窗体中运行良好.
  • 当我从"服务管理"窗口(services.msc)启动和停止服务时,该服务的工作方式就像它应该的那样.

但是在命令行上运行它时:

net start Service1
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

The Service1 service is starting........  The Service1 service could not be started.

The service did not report an error.

More help is available by typing NET HELP:SG 3534.
Run Code Online (Sandbox Code Playgroud)

但奇怪的是,该服务仍然在运行,在服务屏幕中我仍然看到它开始,直到它完全启动.当我试图在之后停止服务时,我得到:

The service could not be controlled in its present state.

More help is available by typing NET HELPMSG 2189
Run Code Online (Sandbox Code Playgroud)

然后服务停止.有没有办法解决这个问题?我已经设法调试服务没有任何问题,代码工作.但是在调试期间,命令控制台上仍然会发生同样的事情,而我仍然可以进一步调试.

这就像onStart()方法有一些超时..我不知道..我对Windows服务相当新(这是我的第一个).我在onStart()方法中编写了所有代码,也许这不是最好的主意,但我不知道在哪里键入它.

如果有人可以提供帮助,我会非常感激.

Joh*_*ais 5

OnStart事件用于启动将负责处理的后台线程.如果OnStart方法完成且没有错误,则服务管理器假定服务已成功启动.因此,OnStart应尽快返回.

然后使用OnStop方法停止后台处理.成功的OnStop告诉服务管理器该服务已关闭且没有错误.