SelectQuery 占用 100% CPU

发布于 2024-08-18 07:21:22 字数 980 浏览 2 评论 0原文

我正在对计算机上的所有用户进行查询,当它执行时,它会占用 100% 的 CPU 并锁定系统。我已经等了 5 分钟,但什么也没发生。

在任务管理器中,wmiprvse.exe 正在使用所有 CPU。当我杀死该进程时,一切都会恢复正常。

这是我的代码:

        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() + "...");
            }
        }

在控制台中我看到的只是获取用户......没有其他任何东西。问题似乎出在 searcher.Get() 上。

有谁知道为什么这个查询占用 100% CPU?谢谢。

编辑:好的,我发现 WMI 进程只占用 25% 的 CPU,但如果我结束程序(查询永远不会完成),它不会被释放。下次我启动一个实例时,该进程的 CPU 利用率会上升到 50%,依此类推,直到达到 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. I have waited up to 5 minutes and nothing happens.

In the Task Manager wmiprvse.exe is using all the 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().

Does anyone know why this query is taking 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). The next time I start an instance the process goes up to 50% CPU, etc, etc until it is at 100%.

So my new question is why is the CPU not getting released and how long should a query like this take to complete?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-08-25 07:21:22

试试这个

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() + "..."); 
  } 
} 

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() + "..."); 
  } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文