简体   繁体   English

扫描Windows进程列表的最有效方法?

[英]Most efficient way to scan the windows process list?

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 1)从流程列表中获取流程可执行文件名称的最有效方法

2) Most efficient way to compare this list to another list 2)比较此列表与另一个列表的最有效方法

For (1), one of the other developers was playing around with using the tasklist command and parsing out the executable names. 对于(1),其他开发人员之一正在使用tasklist命令并解析可执行文件名称。 I also found out that C# has a System.Diagnostic process list that will do this automatically. 我还发现C#有一个System.Diagnostic进程列表,它将自动执行此操作。 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#. 我们仍在尝试在Java和C#之间做出选择,因此我可能会倾向于语言中立的解决方案,但这可能是C#的决定因素。

For (2), supported process list might be small on average (1-10 process names). 对于(2),受支持的进程列表可能平均较小(1-10个进程名称)。 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. 我们可以只运行列表中的每个进程,但是我们认为这对于较旧的PC来说可能太多了,因此我们在考虑使用一个字母顺序平衡的AVL树的想法,该树包含应用程序启动时的初始进程列表,并检查所有内容首先针对它,然后针对树中没有的受支持的进程名称列表进行检查。

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 ? 编辑2:是否有适用于Windows XP Home的任务列表?

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. 如果使用tasklist,实际上只运行一次命令并返回所有结果,而不是为每个可执行文件名称运行它实际上会更快。 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. (当获得结果时,必须在代码中循环遍历它们,但这可能会更快。通常一次运行的进程不会超过100个,因此也不会太糟。)应该编写一个测试并检查是否正确。

In C#, Process.GetProcesses() is the best way to go. 在C#中,Process.GetProcesses()是最好的方法。

In Java, there isn't really an equivalent class/method. 在Java中,实际上没有等效的类/方法。 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. 获取进程列表是特定于OS的,因此Java设计人员必须决定不将此功能集成/抽象到基本Java类中。 You'll probably have to Runtime.getRuntime().exec("tasklist.exe") to get the results on Windows, or exec("ps") on Unix/Linux. 您可能必须要Runtime.getRuntime()。exec(“ tasklist.exe”)才能在Windows上获得结果,而在Unix / Linux上则需要exec(“ ps”)。

Hopefully I understand your question correctly. 希望我能正确理解您的问题。 Are you simply trying to compare a list of processes to active processes running on Windows? 您是否只是试图将进程列表与Windows上运行的活动进程进行比较? C# C#

static void Main(string[] args)
{
    // Approved processes.
    List<string> approvedProcesses = new List<string>();
    approvedProcesses.Add("chrome");
    approvedProcesses.Add("svchost");

    // LINQ query for processes that match your approved processes.
    var processes = from p in System.Diagnostics.Process.GetProcesses()
                    where approvedProcesses.Contains(p.ProcessName)
                    select p;

    // Output matching processes to console.
    foreach (var process in processes)
        Console.WriteLine(process.ProcessName + " " + process.Id.ToString());

    Console.ReadLine();

}

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). 除非基本上保持不变,否则我不会担心过早优化(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. 一个系统通常将运行少于100个进程,即使它有几千个,优化数据结构和设计最有效的算法实际上也不会节省很多时间。

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. 任何瓶颈都可能与Windows询问有关进程的电话有关,因此一次而不是反复进行将是有益的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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