簡體   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