我试图运行一些控制台应用程序,Windows服务,我也跟着this question,我做了一些修改,以使其适合于我的应用程序。C#运行控制台应用程序作为Windows服务 - 该服务没有响应错误
我主要的代码看起来像这样:
public static class Program
{
public class Service : ServiceBase
{
public Service(string serviceName)
{
this.ServiceName = serviceName;
}
protected override void OnStart(string[] args)
{
Program.Start(args);
}
protected override void OnStop()
{
Program.Stop(this.ServiceName);
}
}
#endregion
static void Main(string[] args)
{
if (!Environment.UserInteractive)
// running as service
using (var service = new Service("TestService"))
ServiceBase.Run(service);
else
{
// running as console app
Start(args);
}
}
private static void Start(string[] args)
{
while(true)
{
//DO SOMTHING
}
}
private static void Stop(string serviceName)
{
//Writing to log that 'serviceName' stopped
}
}
我试图运行下面的控制台应用程序作为一种服务,通过使用以下步骤:
1)使用命令:sc创建ServiceTestName123 binPath =“路径到项目调试文件夹中的EXE文件”。
2)使用命令:sc启动ServiceTestName123 “参数1”。
而且我得到了一个错误: “StartService失败1053: 服务没有及时到开始或控制请求”
我了解在互联网上的错误,并发现我可以尝试通过运行另一个线程启动功能来解决这个问题,所以我更新了的OnStart功能下列功能:
protected override void OnStart(string[] args)
{
Thread t = new Thread(() => Program.Start(args));
t.Start();
}
试图重新创建服务后(删除旧的和创建服务再次用新的OnStart函数)并重新运行它,我得到了相同的错误要么。
顺便说一句,当我跑这个代码作为控制台应用程序,一切正常。
可能有人请explaing我什么我做错了?
非常感谢。
这是一个一般错误消息,你应该检查确切的错误信息Windows事件查看器。 – Winnie