[英]Cleaning up after WMI call
We seem to be having a problem where our app that uses WMI calls to get statistics of remote machines is not properly cleaning up after itself. 我们似乎遇到了一个问题,即使用WMI调用获取远程计算机统计信息的应用程序本身无法正确清理。
An example of a call would be: 呼叫的示例为:
public static ManagementObjectCollection getVMs(string target, ManagementObjectCollection collection)
{
ConnectionOptions options = new ConnectionOptions();
options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
ManagementScope scope = new ManagementScope(string.Format("\\\\{0}\\root\\virtualization", target), options);
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM MSVM_ComputerSystem WHERE ElementName like 'TS%'"));
try
{
ManagementObjectCollection ObjCollection = searcher.Get();
collection = ObjCollection;
return collection;
}
catch (Exception e)
{
IOModule.errorWrite(e);
IOModule.debugWrite(e.ToString());
}
return null;
}
This works great, but over time we are noticing that these remote machines are reporting way too many open WMI calls that have not been closed. 这很有效,但是随着时间的流逝,我们注意到这些远程计算机报告了太多尚未关闭的打开的WMI调用。
Where inside of the code should I use using
here? 其中代码里面我应该用
using
吗? Which part of Management
needs to be cleaned up? Management
哪一部分需要清理? Is it ManagementScope
ManagementObjectSearcher
or ManagementObjectCollection
? 是
ManagementScope
ManagementObjectSearcher
还是ManagementObjectCollection
?
Most likely it is the returned collection (judging by the class declaration): 最有可能是返回的集合(根据类声明判断):
public class ManagementObjectCollection : ICollection, IEnumerable, IDisposable
using
should be in the code invoking getVMs
. using
应该在调用getVMs
的代码中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.