I have never used your method, but in my application I use WMI to iterate over processes as follows:
List<ManagementObject> processInfo = processWmi.CreateRequest("SELECT * FROM Win32_Process");
processWmi is a class I use for all of my WMI queries that has extra functionality to kill the query if it is hanging (which WMI seems to do on some servers). The heart of this class is shown below.
private static string _query;
private static string _scope;
private static List<ManagementObject> _data;
private static bool _queryComplete;
private int _timeout = 300;
private static readonly object Locker = new Object();
public List<ManagementObject> CreateRequest(string query, bool eatErrors = false, string scope = null)
{
try
{
lock (Locker)
{
_queryComplete = false;
AscertainObject.ErrorHandler.WriteToLog("Running WMI Query: " + query + " Timeout:" + _timeout, true);
_query = query;
_scope = scope;
Thread serviceThread = new Thread(RunQuery) { Priority = ThreadPriority.Lowest, IsBackground = true };
serviceThread.Start();
int timeLeft = _timeout * 10;
while (timeLeft > 0)
{
if (_queryComplete)
return _data;
timeLeft--;
Thread.Sleep(100);
}
if (eatErrors == false)
AscertainObject.ErrorHandler.WriteToLog("WMI query timeout: " + query, true, "");
serviceThread.Abort();
}
}
catch (Exception ex)
{
if (eatErrors == false)
AscertainObject.ErrorHandler.WriteToLog("Error Running WMI Query", true, ex.ToString());
}
return null;
}
public void SetRequestTimeout(int timeoutSeconds)
{
_timeout = timeoutSeconds;
AscertainObject.ErrorHandler.WriteToLog("WMI query timeout changed to " + timeoutSeconds + " seconds", true);
}
private void RunQuery()
{
try
{
ManagementObjectSearcher searcher = _scope != null ? new ManagementObjectSearcher(_scope, _query) : new ManagementObjectSearcher(_query);
List<ManagementObject> innerData = searcher.Get().Cast<ManagementObject>().ToList();
_data = innerData;
}
catch (Exception ex)
{
AscertainObject.ErrorHandler.WriteToLog("WMI query failed, may have invalid namespace", true, null, true);
_data = null;
}
_queryComplete = true;
}
You can pull the data you want out of the WMI results as follows (type conversions in place to match my Process class):
foreach (ManagementObject item in processInfo)
{
Process tempProcess = new Process
{
id = Convert.ToInt32((UInt32)item["ProcessID"]),
name = (String)item["Name"],
path = (String)item["ExecutablePath"],
parentID = Convert.ToInt32((UInt32)item["ParentProcessID"]),
handleCount = Convert.ToInt32((UInt32)item["HandleCount"]),
priority = Convert.ToInt16((UInt32)item["Priority"]),
threadCount = Convert.ToInt32((UInt32)item["ThreadCount"]),
workingSetMB = Convert.ToInt64((UInt64)item["WorkingSetSize"]) / 1048576,
peakWorkingSetMB = Convert.ToInt64((UInt32)item["PeakWorkingSetSize"]) / 1024,
pageFileUsageMB = Convert.ToInt64((UInt32)item["PageFileUsage"]) / 1024,
peakPageFileUsage = Convert.ToInt64((UInt32)item["PeakPageFileUsage"]) / 1024
};
try
{
//get owner info
object[] ownerInfo = new object[2];
item.InvokeMethod("GetOwner", ownerInfo);
tempProcess.processOwner = (string)ownerInfo[0];
}
catch
{
}
}
WMI results are usually returned very quickly with little to no overhead on the system. They also act similar to SQL queries where you can filter the results with proper WHERE clauses.
Here is the link to all the info you can get back from Win32_Process:
http://msdn.microsoft.com/en-us/library/aa394372(v=vs.85).aspx