-1

I am using code below for getting list of USB devices. But when i run without debugging it shows error:

The application called an interface that was marshalled for a different thread.

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.

public static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
        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; }

    }
Shantnu
  • 127
  • 11
  • Are you using data binding? What line throws the exception? You should use just `throw;` instead of `throw MEEx;` and `throw COMEx;` in your `catch` clauses. – Iuri Farenzena Apr 05 '17 at 18:17
  • No @luri Farenzena – Shantnu Apr 05 '17 at 18:18
  • searcher.Get() Shows Error when i didn't use any exception. and when i use exception it shows in throw MEEx @luri Farenzena – Shantnu Apr 05 '17 at 18:22
  • I am using this method {Provided in link below} to get notify when usb device inserted .http://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events – Shantnu Apr 05 '17 at 18:27
  • 1
    Try to force execution in the UI Thread: `Application.Current.Dispatcher.Invoke(new Action(() => { /* Your code here */ }));` – Iuri Farenzena Apr 05 '17 at 18:29
  • Using the code you posted above, I get no exception whether I execute your `GetUSBDevices()` method in the UI thread or in a worker thread. Your problem is not reproducible with the information provided. Please fix your question so that it includes a good [mcve] that reliably reproduces the problem. – Peter Duniho Apr 05 '17 at 18:33
  • @ Iuri Farenzena: Thanx for consideration, but still same issue – Shantnu Apr 05 '17 at 18:44
  • @ Iuri Farenzena: Thanx for consideration, issue resolved but i use InvokeAsync instead of Invoke – Shantnu Apr 05 '17 at 19:05

1 Answers1

0

I Used

Application.Current.Dispatcher.InvokeAsync(new Action(() => {     IDALUsbDetection.GetUSBDevices(); }));

to call function. Working Fine.

Shantnu
  • 127
  • 11