WMI调用需要太多的时间,当系统启动/重新启动太多、重新启动、系统启动、时间

2023-09-05 04:43:49 作者:王牌女人

我想要得到Windows服务的路径,

I want to get the path of the Windows Service,

var managementObjectSearcher = new ManagementObjectSearcher("Select * from Win32_Service where serviceName = MyService");

这code被写入的一些其他服务的构造......

this code is written of the constructor of some other service...

一切正常,当系统启动并运行,但如果我重新启动系统把秒表类为这个呼叫,它表明,它表明该服务需要35-45秒启动,因为这一呼吁的。

everything works fine when system is UP and running but if i restart the system put the StopWatch class for this call and it shows that it shows that the service takes 35-45 seconds to start because of this call..

任何建议,以提高系统性能的重新启动......

Any suggestions to improve the performance on system restart...

推荐答案

在Windows32_Services类不存在,所以假设你正在使用的 win32_service时 WMI类,你可以提高性能只返回您要使用的属性,在这种情况下,的PathName ,所以改变你的WQL句子

The Windows32_Services class doesn't exist, so assuming which you are using the Win32_Service WMI class you can improve the performance only returning the properties which you want to use, in this case the PathName, so change your WQL sentence to

SELECT PathName FROM Win32_Service Where Name='MyService'

更新

有关 @Bacon 提出的意见是非常正确的,因为你知道检索THES服务的名称,可以建立win32_service时的对象路径看起来像

The observation made for @Bacon is quite correct, since you know the name of thes service to retrieve, you can build the object path of the Win32_Service which look like

Win32_Service.Name="ServiceName"

,然后使用 的ManagementObject 类,你可以用最快的方式获取实例的服务。

And then using the ManagementObject class you can retrieve the instance to the service in a fastest way.