简体   繁体   English

无法在.NET 3.5中使用ServiceController停止服务

[英]Cannot stop the service using ServiceController in .NET 3.5

I'm working in .NET 3.5 and I have the problem with stopping some service using ServiceController. 我在.NET 3.5中工作,使用ServiceController停止某些服务时遇到问题。 I searched whole internet and I found no solution to my problem ;) 我搜索了整个互联网,但没有找到解决问题的办法;)

Here's how I do that: 这是我的方法:

using (ServiceController service = new ServiceController("Service"))
{
    try
    {
        TimeSpan timeout = TimeSpan.FromSeconds(timeoutSeconds);

        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

I'm waiting until my service stops. 我一直等到服务停止。 Then I want to replace it's libraries (update it). 然后我要替换它的库(更新它)。 And the exception is thrown: 并抛出异常:

UnauthorizedAccessException Exception Details: System.UnauthorizedAccessException: Access to the path 'blablabla' is denied. UnauthorizedAccessException异常详细信息:System.UnauthorizedAccessException:拒绝访问“ blablabla”路径。

I'm sure that I have access to this path, because I run my application as an Administrator. 我确定我可以访问此路径,因为我以管理员身份运行应用程序。

What is interesting when this piece of code executes, this Service disappear from the List of current services (in Task Manager). 执行这段代码时,有趣的是,该服务从当前服务列表中消失(在任务管理器中)。 So it actually stops it, but some thread has to still use it, so I can not move any Service's files. 因此它实际上停止了它,但是某些线程仍必须使用它,因此我无法移动任何Service的文件。 Even when I try to move files by myself (I go to the Service directory and try to move its files using mouse) I can not do it. 即使我尝试自己移动文件(我转到“服务”目录并尝试使用鼠标移动其文件),也无法做到。 But when I stop the Service manually (Task Manager and 'End Task') I can do whatever I want with its files. 但是,当我手动停止服务(任务管理器和“结束任务”)时,我可以使用其文件进行任何操作。 So what's the difference between stopping it from C# code (using ServiceController) and stopping it using Task Manager? 那么从C#代码停止它(使用ServiceController)和使用任务管理器停止它之间有什么区别?

I don't know if it's important but I run this application (and this Service) on Windows Server 2008 in Virtual Box. 我不知道它是否重要,但是我在Windows Server 2008的Virtual Box中运行此应用程序(和此服务)。 (I needed to run it on some other machine then my computer). (我需要在其他计算机而非计算机上运行它)。

Any ideas how can I solve this problem? 有什么想法可以解决这个问题吗? ;) ;)

Thanks for any help. 谢谢你的帮助。

Best wishes, Pete. 最好的祝福,皮特。

Ok, I solved my problem. 好,我解决了我的问题。

First I used an Administrator Command-Prompt Command of Net Stop to stop the Service and it worked , but I started to wonder why. 首先,我使用Net StopAdministrator Command-Prompt Command来停止该Service ,该Service可以正常工作 ,但是我开始想知道为什么。 The answer is it took a lot of time! 答案是花了很多时间! ServiceController actually stopped it, but some processes was still using it. ServiceController实际上停止了它,但是某些进程仍在使用它。

 service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

Also workerd fine, because it imidaitely changed the status of the service (it's just one change in Windows Services list), but closing politely all threads using certain Service takes some time :) 也可以很好地工作,因为它可以即时更改服务的状态(这只是Windows Services列表中的一项更改),但是使用某些Service礼貌地关闭所有线程需要一些时间:)

So all You need to do is just wait, and then check again if it's still running. 因此,您需要做的就是等待,然后再次检查它是否仍在运行。 If it is, then kill it with Process class. 如果是这样,则使用Process类将其杀死。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM