简体   繁体   English

SelectQuery占用了100%的CPU

[英]SelectQuery eating up 100% CPU

I am doing a query for all the users on the machine and when it executes it grabs 100% CPU and locks up the system. 我正在对计算机上的所有用户进行查询,当它执行时它会占用100%的CPU并锁定系统。 I have waited up to 5 minutes and nothing happens. 我已经等了5分钟,什么也没发生。

In the Task Manager wmiprvse.exe is using all the CPU. 在任务管理器中,wmiprvse.exe正在使用所有CPU。 When I kill that process everything returns to normal. 当我杀死该进程时,一切恢复正常。

Here is my code: 这是我的代码:

        SelectQuery query = new SelectQuery("Win32_UserAccount", 
            "LocalAccount=1 and Domain='" + GetMachine().DomainName + "'");
        using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) {
            IList<WindowsUser> users = new List<WindowsUser>();
            Console.WriteLine("Getting users...");
            foreach (ManagementObject envVar in searcher.Get()) {
                Console.WriteLine("Getting " + envVar["Name"].ToString() + "...");
            }
        }

In the console all I see is Getting users... and nothing else. 在控制台中,我看到的只是获取用户...而已。 The problem appears to be with searcher.Get(). 问题似乎出在searcher.Get()。

Does anyone know why this query is taking 100% CPU? 有谁知道为什么这个查询占用100%的CPU? Thanks. 谢谢。

EDIT: OK I found that it the WMI process is only eating 25% CPU but it doesn't get released if I end the program (the query never finishes). 编辑:好的,我发现它的WMI进程仅占用25%的CPU,但是如果我结束程序(查询永远不会结束),它不会被释放。 The next time I start an instance the process goes up to 50% CPU, etc, etc until it is at 100%. 下次启动实例时,该过程将占用高达50%的CPU等,直到达到100%。

So my new question is why is the CPU not getting released and how long should a query like this take to complete? 所以我的新问题是,为什么不释放CPU?完成这样的查询需要多长时间?

Try this 尝试这个

SelectQuery query = new SelectQuery("Win32_UserAccount",  "LocalAccount=1 and Domain='" + GetMachine().DomainName + "'"); 
using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) { 
  IList<WindowsUser> users = new List<WindowsUser>(); 
  Console.WriteLine("Getting users...");
  ManagementObjectCollection myCollection = searcher.Get();
  foreach (ManagementObject envVar in MyCollection){ 
    Console.WriteLine("Getting " + envVar["Name"].ToString() + "..."); 
  } 
} 

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

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