扫描Windows进程列表最有效的方法?
所以我目前正在开发一个项目,需要在某些进程运行时计时。 我试图找出扫描进程列表的最有效方法,然后根据支持的程序列表检查进程列表可执行文件名称。
本质上,问题分为两部分:
1)从进程列表中获取进程可执行文件名称的最有效方法
2)将此列表与另一个列表进行比较的最有效方法
对于(1),其他开发人员之一正在尝试使用tasklist 命令并解析出可执行文件名称。 我还发现 C# 有一个 System.Diagnostic 进程列表,可以自动执行此操作。 我们仍在尝试在 Java 和 C# 之间做出选择,因此我可能会倾向于语言中立的解决方案,但这可能是 C# 的决定因素。
对于 (2),支持的进程列表平均可能很小(1-10 个进程名称)。 我们可以只通过列表运行每个进程,但我们认为这对于较旧的 PC 来说可能太多了,因此我们考虑使用按字母顺序平衡的 AVL 树,其中包含应用程序启动时的初始进程列表,并检查所有内容首先对照它,然后检查我们支持的进程名称列表(如果它不在树中)。
非常感谢任何建议。
编辑:显然您可以按进程可执行文件名称过滤任务列表,因此我们可以对支持的进程列表上的每个进程执行此操作。
编辑2:是否有适用于 Windows XP 的等效任务列表家?
So I'm currently working on a project that needs to time when certain processes are running. I'm trying to figure out the most efficient way to scan the process list, then check the process list executable names against the list of supported programs.
Essentially the problem is two parts:
1) Most efficient way to get the process executable names from the process list
2) Most efficient way to compare this list to another list
For (1), one of the other developers was playing around with using the tasklist command and parsing out the executable names. I also found out that C# has a System.Diagnostic process list that will do this automatically. We're still trying to decide between Java and C# so I probably would lean towards a language neutral solution, but this could be a deciding factor for C#.
For (2), supported process list might be small on average (1-10 process names). We could just run each process through the list, but we were thinking this might be too much for older PCs, so we were tossing around the idea of using an alphabetically balanced AVL tree containing the initial process list when the application starts, and checking everything against that first, and then checking against our supported process names list if its not in the tree.
Any suggestions are greatly appreciated.
Edit: Apparently you can filter tasklist by process executable name, so we could just do that for every process on the supported process list.
Edit 2: Is there a tasklist equivalent that works for Windows XP Home ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用任务列表,实际上只运行一次命令并返回所有结果可能比为每个可执行文件名称运行它更快。 执行进程并获取输出会产生一些开销。 (当您返回结果时,您必须在代码中循环遍历它们,但这可能会更快。通常一次运行的进程不会超过 100 个,因此不会太糟糕。)应该编写一个测试并检查是否属实。
在 C# 中,Process.GetProcesses() 是最好的方法。
在 Java 中,并没有真正等效的类/方法。 获取进程列表是特定于操作系统的,因此 Java 设计者一定决定不将此功能集成/抽象到 Java 基类中。 您可能需要在 Windows 上使用 Runtime.getRuntime().exec("tasklist.exe") 来获取结果,或者在 Unix/Linux 上使用 exec("ps") 来获取结果。
If you go with tasklist, it might actually be faster to just run the command once and get back all of the results, rather than running it for each of your executable names. There is some overhead for exec'ing a process, and getting the output. (When you get back the results, you'll have to loop through them in code, but this might be faster. Normally there won't be more than 100 processes running at once, so it won't be too bad.) You should write a test and check to see if that's true.
In C#, Process.GetProcesses() is the best way to go.
In Java, there isn't really an equivalent class/method. Getting a process list is pretty OS-specific, so the Java designers must have decided not to integrate/abstract this capability into the base Java classes. You'll probably have to Runtime.getRuntime().exec("tasklist.exe") to get the results on Windows, or exec("ps") on Unix/Linux.
希望我正确理解你的问题。 您是否只是想将进程列表与 Windows 上运行的活动进程进行比较? C#
Hopefully I understand your question correctly. Are you simply trying to compare a list of processes to active processes running on Windows? C#
您多久进行一次这项检查? 除非它基本上是恒定的,否则我不会担心过早优化(SO 的常见主题)。
一个系统运行的进程通常少于 100 个,即使有几千个,优化数据结构和设计最有效的算法也不会为你节省太多时间。
话虽如此,我建议您获取所有正在运行的进程,并将该列表与内存中批准的列表进行比较。 任何瓶颈都可能与调用 Windows 询问进程有关,因此执行一次而不是重复执行此操作将是有益的。
How often will you be doing this check? Unless it's basically constant, I wouldn't worry about optimizing too early (a common theme on SO).
A system will usually have less than 100 processes running, and even if it has a couple of thousand, optimizing your data structures and devising the most efficient algorithms really won't save you much time.
Having said that, I would suggest that you get all running processes and compare that list to your approved list in memory. Any bottleneck will probably be with the call to Windows asking about the processes, so doing this once rather than repeatedly will be beneficial.