WMI to return all data within a time limit, or else throw an error

  • Thread starter Thread starter JFoushee
  • Start date Start date
J

JFoushee

Guest
(I put this in the WMI forum because it has more to do with WMIs nuances than C#)


Hi. I built a multi-threaded C# program making WMI calls on my local machine, and I was pleased with it.

I then placed my program in a Stress-Test environment, and found some strange behaviors. WMI datatables were coming back empty with no errors, when I know rows should be returned. (I audit the values as they change, so I am seeing a value switch to NULL, then back to value later in the day)

This led to investigating the ReturnImmediately, Rewindable and Timeout options for the ManagementObjectSearcher class.

Using the following combinations, I get the following behaviors:

public DataTable Query(string WQLQuery, int TimeOut = 20)
{
if (m_myManagementScope == null)
{
throw new Exception("WMI Connection not connected yet.");
}

DataTable dtReturn = new DataTable("Results");

ObjectQuery oQuery = new System.Management.ObjectQuery(WQLQuery);

using (ManagementObjectSearcher querySearcher = new ManagementObjectSearcher(m_myManagementScope, oQuery))
{
querySearcher.Options.ReturnImmediately = true;
querySearcher.Options.Rewindable = false;
querySearcher.Options.Timeout = new TimeSpan(0, 0, TimeOut);

ManagementObjectCollection moc = null;
try
{
moc = GoGet(querySearcher);

As mentioned earlier, the above combination respects the timeout limit, but returns empty datatables if the timeout is reached. For the most part, I can safely ignore some of these datatables if I am guaranteed at least one row. But on parent-child datatables (eg. MSCluster_ResourceGroupToPreferredNode) I cannot make a decision about the non-existence of rows.

public DataTable Query(string WQLQuery, int TimeOut = 20)
{
if (m_myManagementScope == null)
{
throw new Exception("WMI Connection not connected yet.");
}

DataTable dtReturn = new DataTable("Results");

ObjectQuery oQuery = new System.Management.ObjectQuery(WQLQuery);

using (ManagementObjectSearcher querySearcher = new ManagementObjectSearcher(m_myManagementScope, oQuery))
{
querySearcher.Options.ReturnImmediately = false;
querySearcher.Options.Rewindable = false;
querySearcher.Options.Timeout = new TimeSpan(0, 0, TimeOut);

ManagementObjectCollection moc = null;
try
{
moc = GoGet(querySearcher);

Whereas the above combination gets the correct data, it potentially can hand indefinitely.


Im looking for something that can do the following:

- Return all associated data requested by the WMI call, if, and only if, it can be returned in the time limit

- If the timeout limit is reached, throw an error.


Do I need to adjust my ManagementObjectSearcher options, or is this impossible?

Continue reading...
 
Back
Top