WMI调用后清理

问题描述:

我们似乎遇到了一个问题,即使用WMI调用来获取远程计算机统计信息的应用程序本身没有正确清理。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的哪一部分?它是ManagementScopeManagementObjectSearcherManagementObjectCollection

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

public class ManagementObjectCollection : ICollection, IEnumerable, IDisposable 

using应该在代码调用getVMs