C# .NET - WMI query hangs on ManagementObjectCollection

Asked By Clark Bohs on 01-Jul-09 01:17 PM

Hello,

I have an app that loops through a list of machines and queries their WMI database.

On some machines, WMI is not working correctly and as such my app "hangs" forever on the following line:

        System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();

How can I by-pass this if it fails?  I'm using a "try" but it doesn't throw an exception when it hits this line and hangs.

At this point I don't care that WMI is not working, I just want my app to throw an exception via "catch" and continue to the next machine.

Thanks!

Clark Bohs

 

 

 

This article shows how you can make any method

Peter Bromberg replied to Clark Bohs on 01-Jul-09 04:09 PM

Re

Ravenet Rasaiyah replied to Clark Bohs on 01-Jul-09 11:03 PM
Hi

Yes you can catch Management Exception in about operation. Here my sample code and my articles


public static string GetDriveSerialNO(string logicaldriveName)
{
if (string.IsNullOrEmpty(logicaldriveName))
{
logicaldriveName = "C";
}
// create management object to invoke the WMI query to access the drive.
try
{
ManagementObject hdDisk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"");
hdDisk.Get();
// read the volume serial no value using key from the collections
string serialNo = hdDisk["VolumeSerialNumber"].ToString();
// fianlly dispose the management object.
hdDisk.Dispose();
return serialNo;
}
catch (System.Management.ManagementException mex)
{

throw mex;
}
}

Here my article http://www.codegain.com/index.php?option=com_sectionex&view=category&id=5&Itemid=18#catid167

Thank you
http://www.codegain.com

WMI query hangs on ManagementObjectCollection

Clark Bohs replied to Peter Bromberg on 02-Jul-09 09:44 AM

Thanks Peter!  I'll try it out and post the results.

Clark Bohs

try and catch do not work in this case
Clark Bohs replied to Ravenet Rasaiyah on 02-Jul-09 09:46 AM

Thanks for your response Bill, but unfortunately it doesn't match what I'm asking.

If "try catch" worked then I wouldn't have posted my question in the first place.

Below is my original question...

I have an app that loops through a list of machines and queries their WMI database.

On some machines, WMI is not working correctly and as such my app "hangs" forever on the following line:

        System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();

How can I by-pass this if it fails?  I'm using a "try" but it doesn't throw an exception when it hits this line and hangs.

At this point I don't care that WMI is not working, I just want my app to throw an exception via "catch" and continue to the next machine.

WMI query hangs on ManagementObjectCollection
Clark Bohs replied to Clark Bohs on 02-Jul-09 01:43 PM

Hello again Peter,

I took a look at your timeout code and I'm having some difficulty porting the functionality to my application.

Can you help me integrate your code with the code below?  Thanks!

try

{

//This is where I want the timeout to be.  if the next line doesn't respond in 5 seconds, then by-pass it and go to the next machine in the loop.

System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();

}

catch

{

}



WMI query hangs on ManagementObjectCollection (solution)
Clark Bohs replied to Clark Bohs on 04-Sep-09 01:32 PM

I answered my own question.  I created a new thread and started a method within the new thread and set a time-out on the join.

See code below.  It captures the machine name where it timed out and then continues on with the processing of the main thread.  I didn't include the method since any method can be plugged in, along with the appropriate method parameters.

Thread trd = new Thread(new ThreadStart(delegate { WMIMethod(username, password, numOfLines1, machine, numberOfLines); }));

trd.Start();

if (!trd.Join(20000))

{

string timedout = ".\\logs\\timedout.txt";

// Specify file, instructions, and privileges

FileStream file = new FileStream(timedout, FileMode.Append, FileAccess.Write);

// Create a new stream to write to the file

StreamWriter sw = new StreamWriter(file);

// Write a string to the file

sw.WriteLine(machine);

// Close StreamWriter

sw.Close();

// Close file

file.Close();

Console.WriteLine(machine + " timed out " + numberOfLines + " of " + numOfLines1);

}

trd.Join();

WMI requests that hangs the program
Morten Skaarup replied to Clark Bohs on 23-Dec-09 03:23 AM
I have excatly the same problem. Did you find a solution?
WMI query hangs on ManagementObjectCollection
Clark Bohs replied to Morten Skaarup on 23-Dec-09 09:28 AM

Yes, I answered my own question and posted it.  You replied to the solution I posted.

Here it is again....


I answered my own question.  I created a new thread and started a method within the new thread and set a time-out on the join.


See code below.  It captures the machine name where it timed out and then continues on with the processing of the main thread.  I didn't include the method since any method can be plugged in, along with the appropriate method parameters.


Thread trd = new Thread(new ThreadStart(delegate { WMIMethod(username, password, numOfLines1, machine, numberOfLines); }));

trd.Start();


if (!trd.Join(20000))

{


string timedout = ".\\logs\\timedout.txt";

// Specify file, instructions, and privileges

FileStream file = new FileStream(timedout, FileMode.Append, FileAccess.Write);

// Create a new stream to write to the file

StreamWriter sw = new StreamWriter(file);



// Write a string to the file

sw.WriteLine(machine);


// Close StreamWriter

sw.Close();


// Close file

file.Close();


Console.WriteLine(machine + " timed out " + numberOfLines + " of " + numOfLines1);

}


trd.Join();