无法手动启动 C# Windows 服务
Unable to Manually Start C# Windows Service
我在 Windows 10 笔记本电脑(从 Windows 7 升级并完全更新)上在 Visual Studio 社区 2015 中创建了一个 Windows 服务项目,但我有无法手动成功启动我的服务,而且我无法在服务开始启动后强制终止该服务。当我尝试通过服务控制台 (services.msc) 启动我的服务时,出现以下行为:
- A "Service Control" dialog pops up with a progress bar.
- The progress bar fills slowly over the course of 90 seconds.
- When the progress bar is full, I receive Error 1053.
- The service's state changes to and remains infinitely at "Starting" (Start Pending).
- The service can not be stopped by any means besides a forced reboot.
我了解到错误 1053 是服务在 30 秒后无法启动的结果。但是,我的服务启动时间不可能超过 30 秒。我的OnStart
、OnStop
、OnTimer
方法目前如下:
protected override void OnStart(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
protected override void OnStop()
{
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
}
我的 Main
方法如下所示:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyNewService()
};
ServiceBase.Run(ServicesToRun);
}
奇怪的是,如果我的服务的启动方法设置为 "Automatic" 那么下次 Windows 重新启动时服务确实会自行启动。当它自动启动时,我可以手动停止服务(这是唯一一次手动停止)。但是,在成功手动停止后,我仍然无法手动重新启动它。尝试这样做会产生与前面描述的完全相同的行为,再次导致错误 1053。
我的服务安装没有错误(通过 installutil),我可以卸载没有错误。但是,如果我尝试卸载我的服务(通过 installutil /u),而服务停留在 "Starting"(启动挂起)模式,服务不会停止,保持为 "Starting",并且启动模式切换到 "Disabled"。只有在重新启动时,该服务才会完全删除。
我的问题:
为什么我的服务在 Windows 启动时自动启动,而不是通过任何其他手动方法启动? (我需要它自动启动,但也能手动启动和停止它。)
感谢您的宝贵时间和专业知识。
您应该在方法 OnStop
中清理服务。
你创造了一个 timer
并且永远不会停止它。
private System.Timers.Timer timer;
protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer = null;
}
问题是 Avast anti-virus 静默停止服务。令人恼火的是,Avast 不会将该服务报告为病毒或任何类型的威胁,也不会记录被阻止的服务。禁用 Avast 后,我能够手动成功启动和停止我的服务。作为 semi-permanent 解决方案,我已将我的服务路径添加到 Avast 的例外列表中,方法是转到“设置”->“主动保护”->“自定义(文件系统防护)”->“排除”,然后单击 "Add."
为了回答我最初的问题,我的服务能够在 Windows 启动时自动启动,因为它能够在加载 Avast 之前启动。这导致了我观察到的奇怪行为,我可以在它自动启动后停止它,但不能手动重新启动它,因为那时 Avast 现在 运行。
我在 Windows 10 笔记本电脑(从 Windows 7 升级并完全更新)上在 Visual Studio 社区 2015 中创建了一个 Windows 服务项目,但我有无法手动成功启动我的服务,而且我无法在服务开始启动后强制终止该服务。当我尝试通过服务控制台 (services.msc) 启动我的服务时,出现以下行为:
- A "Service Control" dialog pops up with a progress bar.
- The progress bar fills slowly over the course of 90 seconds.
- When the progress bar is full, I receive Error 1053.
- The service's state changes to and remains infinitely at "Starting" (Start Pending).
- The service can not be stopped by any means besides a forced reboot.
我了解到错误 1053 是服务在 30 秒后无法启动的结果。但是,我的服务启动时间不可能超过 30 秒。我的OnStart
、OnStop
、OnTimer
方法目前如下:
protected override void OnStart(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
protected override void OnStop()
{
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
}
我的 Main
方法如下所示:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyNewService()
};
ServiceBase.Run(ServicesToRun);
}
奇怪的是,如果我的服务的启动方法设置为 "Automatic" 那么下次 Windows 重新启动时服务确实会自行启动。当它自动启动时,我可以手动停止服务(这是唯一一次手动停止)。但是,在成功手动停止后,我仍然无法手动重新启动它。尝试这样做会产生与前面描述的完全相同的行为,再次导致错误 1053。
我的服务安装没有错误(通过 installutil),我可以卸载没有错误。但是,如果我尝试卸载我的服务(通过 installutil /u),而服务停留在 "Starting"(启动挂起)模式,服务不会停止,保持为 "Starting",并且启动模式切换到 "Disabled"。只有在重新启动时,该服务才会完全删除。
我的问题: 为什么我的服务在 Windows 启动时自动启动,而不是通过任何其他手动方法启动? (我需要它自动启动,但也能手动启动和停止它。)
感谢您的宝贵时间和专业知识。
您应该在方法 OnStop
中清理服务。
你创造了一个 timer
并且永远不会停止它。
private System.Timers.Timer timer;
protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer = null;
}
问题是 Avast anti-virus 静默停止服务。令人恼火的是,Avast 不会将该服务报告为病毒或任何类型的威胁,也不会记录被阻止的服务。禁用 Avast 后,我能够手动成功启动和停止我的服务。作为 semi-permanent 解决方案,我已将我的服务路径添加到 Avast 的例外列表中,方法是转到“设置”->“主动保护”->“自定义(文件系统防护)”->“排除”,然后单击 "Add."
为了回答我最初的问题,我的服务能够在 Windows 启动时自动启动,因为它能够在加载 Avast 之前启动。这导致了我观察到的奇怪行为,我可以在它自动启动后停止它,但不能手动重新启动它,因为那时 Avast 现在 运行。