微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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事件用于启动进程,耗时的进程将执行它在子线程中的执行.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐