繁体   English   中英

WMI通话后清理

[英]Cleaning up after WMI call

我们似乎遇到了一个问题,即使用WMI调用获取远程计算机统计信息的应用程序本身无法正确清理。

呼叫的示例为:

    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;
    }

这很有效,但是随着时间的流逝,我们注意到这些远程计算机报告了太多尚未关闭的打开的WMI调用。

其中代码里面我应该用using吗? Management哪一部分需要清理? ManagementScope ManagementObjectSearcher还是ManagementObjectCollection

最有可能是返回的集合(根据类声明判断):

public class ManagementObjectCollection : ICollection, IEnumerable, IDisposable

using应该在调用getVMs的代码中。

暂无
暂无

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

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