I have the following code that checks the status of a service on a remote computer. The problem is that if the remote computer cannot be found (he or something else), then the method ManagementObjectSearcher.Get()takes 20 seconds to give the error "RPC server is unavailable." In cases where the server is unavailable, I want to explicitly indicate that I want it to be tried for a short period of time (e.g. 3 seconds). I followed the post here , but it states that it should use the Timeout parameter for ManagementObjectSearcher, but my code seems to ignore this value (since it states that this does not apply to collections). Is there something that I am missing on these parameters? I tried using the property , but to no avail. ReturnImmediatly
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);
var managementObjectCollection = searcher.Get();
var serviceState = managementObjectCollection.Cast<ManagementObject>().ToList().Single()["State"].ToString();
}
catch (Exception ex)
{
}
}