Link to home
Start Free TrialLog in
Avatar of nemakcanada
nemakcanada

asked on

WMI ManagementObjectSearcher Options

I'm running WMI queries on several machines on our network. What I am finding is that although a group of machines (same make, model, hardware, software platform) are physically located the same distance from me, and are plugged into the same switch, I'm getting large differences in the return time of the following query:

SELECT * FROM Win32_OperatingSystem

I generally get results in roughly 6 seconds, but sometimes the result return takes 1min+ . I'm trying to figure out if I can just force a timeout on a query using the Options property of the ManagementObjectSearcher class, but I'm not sure what options can be set here. Does anyone have any other thoughts why the delays may occur? It's not always the same delay length or from the same computers. It seems like it just randomly pauses. Our network usage hovers at a low 2%, and collisions are little to none, so I'm sure it's not a network issue. The TimeOut property of the Connection class doesn't work, cause I connect just fine, its the call to ManagementObjectSearcher.Get() that causes the delay. Thanks!

Brian
Avatar of gav_jackson
gav_jackson

funnily enough i am writing a similar WMI scanner.

I also noticed a big and differential delay between scans...i am not sure of the actual reason for this but i do have a solution.

use multi-threading, as in for each machine queue a call to the scan class in a new thread in the threadpool, this will automatically queue all your scans and will send out up to 25scans at the same time, ie you should recieve your results 25 times faster than before.

check out

http://www.yoda.arachsys.com/csharp/threadstart.html

for more info on threads
Avatar of nemakcanada

ASKER

Good advice, I'm going to have to play around with the Thread priorities and sleep times cause this approach bumped up my CPU usage by 10 times. Ok, so here's what I have:

Main Application Thread
       ->creates multiple scanning threads

Suppose I want the application to terminate when scan is complete, how can I determine when all the the Threads I have created, are complete. I realize that there is an IsAlive property for each thread, but I'm basically creating my Threads like this:

while(this.current_machine_index < 200)
{
      RootScanner root = new RootScanner(ip);
      Thread scan_thread = new Thread(new ThreadStart(root.RunWMIScan));
      scan_thread.Priority = ThreadPriority.BelowNormal;
      scan_thread.Start();
}

When I exit this loop, scan_thread no longer references the 195th thread I created for example, it now references thread 199, therefore scan_thread.IsAlive no refers to thread 199. Suppoer 199 returns faster that 195, I'd incorrectly terminiate the app. Any thoughts?
       

why not create a simple array of Threads ?

somewhere past the class declaration
private Thread[] scan_thread;

somewhere in Form_Load or constructor:
scan_thread = new scan_thread[200];

somewhere in your code:
while (...)
{
...
scan_thread[index] = new Thread(ThreadStart(.......))
scan_thread[index].Start;
...
}


or am I completely missing the point ?
ASKER CERTIFIED SOLUTION
Avatar of gav_jackson
gav_jackson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
somnatic.

I could create an array, but due to memory consumption issues, I'd rather not. The array could be anywhere from 300-1000 items in size for our internal network alone, let alone if we start using a scanner like this throughout the company 3000+.

gav_jackson

Good call on checking the number of threads in the pool, it'll work good enough for me. I've created a configuration app for my scanner that will allow an end user to throttle the speed/cpu usage up and down based on a few things. By default, it uses the ThreadPool class, but it allows the user (an admin) to configure how many threads (1-24) and how long of a delay between thread execution so that no matter what machine we run this on, we can keep CPU usage down. Thanks for your help on this, I'll award you the points.