title: The application called an interface that was marshalled for a different thread.When getting list of USB_Hub
body: <p>I am using code below for getting list of USB devices. But when i run without debugging it shows error: </p>
  
  <p>The application called an interface that was marshalled for a different thread.</p>
  
  <p>But when i run application with debugging it works fine. I m using this function in separate class library project and calling it on WPF form.</p>
  
  <pre><code>public static List&lt;USBDeviceInfo&gt; GetUSBDevices()
      {
          List&lt;USBDeviceInfo&gt; devices = new List&lt;USBDeviceInfo&gt;();
          ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
          ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_USBHub");
          try
          {
              ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
              ManagementObjectCollection queryCollection = searcher.Get();
              //ManagementObjectCollection collection;
              //using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
              //var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
              //collection = searcher.Get();
  
              foreach (ManagementObject device in queryCollection)
              {
                  devices.Add(new USBDeviceInfo(
                  (string)device.GetPropertyValue("DeviceID"),
                  (string)device.GetPropertyValue("PNPDeviceID"),
                  (string)device.GetPropertyValue("Description"),
                  (string)device.GetPropertyValue("Name")
                  ));
              }
          }
          catch (ManagementException MEEx)
          {
              throw MEEx;
          }
          catch(COMException COMEx)
          {
              throw COMEx;
          }
          finally
          {
  
          }
          //collection.Dispose();
          return devices;
      }
  
      public class USBDeviceInfo
      {
          public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name)
          {
              this.DeviceID = deviceID;
              this.PnpDeviceID = pnpDeviceID;
              this.Description = description;
              this.Name = name;
          }
          public string DeviceID { get; private set; }
          public string PnpDeviceID { get; private set; }
          public string Description { get; private set; }
          public string Name { get; private set; }
  
      }
  </code></pre>