C#C
C#2y ago
Dingus

WPF possible threading issue?

I am creating a application to update a bunch of usb devices at the same time. I have figured out the usb communication with devices just fine but somehow someway I get a threading issue. I am just getting into threading so I just need someone with personal experience on how they would handle this situation.

I have a System.Management object to watch for usb events. When usb connect/disconnect event fires off then check all devices connected by specific usb protocol and look at device paths for PID/VID (specific to manufacturer).

public MainWindow()
        {
            InitializeComponent();
            StartDeviceDetection();
        }

public void StartDeviceDetection()//start device detection through USB
        {
            ManagementEventWatcher watcher = new ManagementEventWatcher();
            WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); //Querey for specific device standard
            watcher.EventArrived += USBEvents; //subscribe to events it should querey for
            watcher.Query = query; //Set Query
            watcher.Start();//start watch
        }

 private void USBEvents(object sender, EventArrivedEventArgs e)//detects when device is removed from computer
        {
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
                eventType = e.NewEvent.ClassPath.ClassName;
            //scanning for device arrival
            if (eventType.Equals("__InstanceCreationEvent"))
            {
                aDeviceArrived();
            }
        }

private void aDeviceArrive(){
        List<string> allPaths = {list of all paths with specific}
        foreach(string path in allPaths){
             if(path.Contains(VID) && path.Contains(PID)){
                  Device device = new Device();
                  device.path = path;
                  gridWithDevices.add(device);
             }
}
Was this page helpful?