I have an application which keeps track of Windows process IDs and need to know whether a certain process ID is still running. Process xxxxx, is it running, true or false? I have a roundabout way of doing this in C# which I'm not happy with, namely
static long[] Processes() {
long[] processes = new long[100000];
ManagementClass m = new ManagementClass("Win32_Process");
foreach (ManagementObject mobj in m.GetInstances()) {
long t = (UInt32)mobj["ProcessId"];
processes[t] = t;
}
return processes;
}
The idea here is that since there are only 100000 possible process ids, the function returns an array of 100000 longs. If process id xxxxx is running, then processids[xxxxx] is not zero. I refresh this list every minute or so. Yes, I know that there is always the risk that process id xxxxx may have been freshly started and it would count as a false positive.
Certainly there is a better (and faster) way?