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. 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开发者_JAVA技巧%.
So my new question is why is the CPU not getting released and how long should a query like this take to complete?
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() + "...");
}
}
精彩评论