c# - The application called an interface that was marshalled for a different thread.When getting list of USB_Hub -


i using code below getting list of usb devices. when run without debugging shows error:

the application called interface marshalled different thread.

but when run application debugging works fine. m using function in separate class library project , calling on wpf form.

public static list<usbdeviceinfo> getusbdevices()     {         list<usbdeviceinfo> devices = new list<usbdeviceinfo>();         managementscope scope = new managementscope("\\\\.\\root\\cimv2");         objectquery query = new objectquery("select * win32_usbhub");         try         {             managementobjectsearcher searcher = new managementobjectsearcher(scope, query);             managementobjectcollection querycollection = searcher.get();             //managementobjectcollection collection;             //using (var searcher = new managementobjectsearcher(@"select * win32_usbhub"))             //var searcher = new managementobjectsearcher(@"select * 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;         }                 {          }         //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; }      } 

i used

application.current.dispatcher.invokeasync(new action(() => {     idalusbdetection.getusbdevices(); })); 

to call function. working fine.


Comments