简体   繁体   中英

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.

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.

Where inside of the code should I use using here? Which part of Management needs to be cleaned up? Is it ManagementScope ManagementObjectSearcher or 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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