.net - Fastest & Least CPU intensive way to Get List of Running Processes with their Id and Location in C# -
i have written module gets list of running processes every 250ms windows xp , above. have tried .net & wmi way, both of them cpu intensive. both of them finish within 80ms on machine. host process cpu remains above 10 14 percentage in either case. think location/executionpath property real culprit. there better way information?
edit1: in testing - .net way more cpu intensive faster wmi way. - wmi way slower less cpu intensive since moved cpu usage wmi provider host
private static int wmiway() { string wmiquerystring = "select processid, executablepath win32_process"; using (var searcher = new managementobjectsearcher(wmiquerystring)) { using (var results = searcher.get()) { foreach (managementobject oreturn in results) { if (oreturn["executablepath"] != null) { _processes.add(new processinfo() { processid = (uint)oreturn["processid"], filepath = oreturn["executablepath"].tostring(), }); } } return results.count; } } } private static int netway() { var processes = system.diagnostics.process.getprocesses(); foreach (system.diagnostics.process runningprocess in processes) { if (runningprocess.id > 4) { try { _processes.add(new processinfo() { processid = (uint)runningprocess.id, filepath = runningprocess.mainmodule.filename, }); } catch { } } } return processes.length; }
finally decided go pinvoke route. in testing turn out best solution. did reduce cpu usage 2 percentage , faster .net or wmi way.
private static int pinvokeway() { uint[] processids = new uint[1024]; uint bytescopied; uint processcount = 0; if (processapi.enumprocesses(processids, (uint)processids.length * sizeof(uint), out bytescopied) || bytescopied == 0) { processcount = bytescopied / sizeof(int); (int = 0; < processcount; i++) { string path; if (environment.osversion.version.major >= 6) path = getexecutablepathabovevista(processids[i]); else path = getexecutablepathxp2003(processids[i]); if (!string.isnullorwhitespace(path)) { _processes.add(new processinfo() { processid = processids[i], filepath = path, }); } } } return (int)processcount; } private static string getexecutablepathabovevista(uint processid) { var buffer = new stringbuilder(1024); try { intptr hprocess = processapi.openprocess(processapi.process_query_limited_information, false, processid); if (hprocess != intptr.zero) { try { int size = buffer.capacity; if (processapi.queryfullprocessimagename(hprocess, 0, buffer, out size)) { return buffer.tostring(); } } { processapi.closehandle(hprocess); } } } catch { } return string.empty; } private static string getexecutablepathxp2003(uint processid) { var buffer = new stringbuilder(1024); try { intptr process = processapi.openprocess(processapi.process_query_information | processapi.process_vm_read, false, processid); if (process != intptr.zero) { try { if (processapi.getmodulefilenameexw(process, intptr.zero, buffer, buffer.capacity) != 0) { return buffer.tostring(); } } { processapi.closehandle(process); } } } catch { } return string.empty; }
Comments
Post a Comment