c# – 停止Windows服务时出错

我正在使用 Windows服务.工作正常当我尝试从services.msc停止服务时,会引发以下错误

Windows Could not stop the xxx service on Local Computer.
The service did not return an error. This Could be an internal Windows error or an internal service error.
If the problem persists,contact your system administrator.

如果我再次尝试停止它,它需要很多时间,然后抛出错误如下:

Windows Could not stop the xxx service on Local Computer.
Error 1061: The service cannot accept control messages at this time.

此时,服务已停止.但是如果我尝试重新安装该服务,它会引发另一个错误

Windows Could not stop the xxx service on Local Computer.
Error 1001: The specified service is marked for deletion.

关闭services.msc之后,它可以让我重新安装服务,再次,事情开始工作正常.

在OnStop()中,我做了一些冗长的操作,需要时间才能完成.

有什么想法可以让整个事情顺利进行吗?

– 编辑 –

这是我的OnStop方法的样子:

protected override void OnStop()
{
    base.OnStop();
    //Some lengthy operation      
}

解决方法

Windows服务在onstart和onstop事件中具有认超时.通常,如果您在任何这些事件中执行耗时的操作,则会启动一个新线程,并让操作在后台执行.

通常,Windows服务中的OnStart以及OnStop事件用于启动进程,耗时的进程将执行它在子线程中的执行.

希望这将解决您的问题..

相关文章

C#项目进行IIS部署过程中报错及其一般解决方案_c#iis执行语句...
微信扫码登录PC端网站应用的案例(C#)_c# 微信扫码登录
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...