title: How to set a timeout on WMI queries?
body: <p>I have a .NET application which runs WMI queries on all domain computers in order to find the logged in user; it pings each computer to find whether it is online or not, then runs the actual query.</p>
  
  <p>Code snippet:</p>
  
  <pre><code>try
  {
      string loggedonuser = null;
  
      string computername = "ComputerToQuery";
  
      ConnectionOptions co = new ConnectionOptions();
  
      co.Username = "DOMAIN\MyUser";
      co.Password = "MyPassword";
  
      co.Impersonation = ImpersonationLevel.Impersonate;
      co.Authentication = AuthenticationLevel.Default;
  
      ManagementPath mp = new ManagementPath(@"\\" + computername + @"\root\cimv2");
  
      ManagementScope ms = new ManagementScope(mp,co);
  
      ms.Connect();
  
      ObjectQuery oq = new ObjectQuery("SELECT username FROM Win32_ComputerSystem");
  
      ManagementObjectSearcher mos = new ManagementObjectSearcher(ms,oq);
  
      foreach(ManagementObject mo in mos.Get())
          loggedonuser = (String) mo["username"];
  }
  catch(Exception e)
  {
      // Handle WMI exception
  }
  </code></pre>
  
  <p>The problem: sometimes the WMI query hangs on indefinitely.</p>
  
  <p>How can I set a timeout on it?</p>