Search code examples
c#timeoutwmi

Set Timeout On Remote WMI when RPC Server Is Unavailable


I have the following code that checks the status of a service on a remote computer. The problem is that if the remote computer can't be found (it's down or something), then the ManagementObjectSearcher.Get() method takes 20 seconds to throw an error of "The RPC Server Is Unavailable". In cases where the server is unavailable, I want to explicitly state that I only want it to try for a short period of time (like 3 seconds). I have followed the post here, but it states to use the Timeout option on ManagementObjectSearcher, but my code seems to ignore that value (as it states it's not relevant on collections). Is there something I'm overlooking on these options? I have tried using the ReturnImmediatly property as well to no avail.

public static void WmiServiceCheck()
    {
        try
        {
            var computerName = "SomeInvalidComputer";
            var serviceName = "Power";
            var managementScope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", computerName));
            var objectQuery = new ObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName));
            var searcher = new ManagementObjectSearcher(managementScope, objectQuery);
            searcher.Options.Timeout = new TimeSpan(0, 0, 0, 3); // Timeout of 3 seconds
            var managementObjectCollection = searcher.Get();
            var serviceState = managementObjectCollection.Cast<ManagementObject>().ToList().Single()["State"].ToString();
            /// Other stuff here
        }
        catch (Exception ex)
        {
        }
    }

Solution

  • Yes, not that one. You want to set the ConnectionOptions.Timeout:

      var managementScope = new ManagementScope(...);
      managementScope.Options.Timeout = TimeSpan.FromSeconds(3);
    

    Worked well when I tested it.

    Do keep in mind that 3 seconds is on the low end, the server may well have to swap a lot of code into RAM to handle the request if it hasn't been queried for a while. That's not necessarily a speedy operation if the server is otherwise keeping the disk drive hoppin'. Only pick it if you don't mind the occasional false alarm. I personally never go below 10 seconds on a workstation, learned in the School of Hard Knocks. 20 seconds for a server class machine is safe.