【发布时间】:2018-04-19 06:49:58
【问题描述】:
我创建了两个处理程序来接收 USB 设备插入/拔出事件,类似于 this answer。
private void RegisterUSBEvents()
{
WqlEventQuery insertQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'");
ManagementEventWatcher insertWatcher = new ManagementEventWatcher(insertQuery);
insertWatcher.EventArrived += new EventArrivedEventHandler(DeviceInsertedEvent);
insertWatcher.Start();
WqlEventQuery removeQuery = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'");
ManagementEventWatcher removeWatcher = new ManagementEventWatcher(removeQuery);
removeWatcher.EventArrived += new EventArrivedEventHandler(DeviceRemovedEvent);
removeWatcher.Start();
}
我可以使用上面的代码接收任何 USB 插拔事件。
对于大多数 PC,在 Windows 从休眠状态唤醒后,我的应用将收到 InstanceDeletionEvent,然后是 USB 设备的 InstanceCreationEvent。
但是,在少数计算机上,Windows 休眠和唤醒后,我只能收到InstanceDeletionEvent,但没有InstanceCreationEvent 用于同一设备。 USB 设备在 USB 端口上保持不变。
如果我关闭并重新启动我的应用程序,或者拔下/插入同一设备,上述代码将再次起作用。
是否有任何问题导致 USB 设备在某些环境下休眠后会触发 InstanceDeletionEvent 但不会触发 InstanceCreationEvent?
【问题讨论】:
标签: c# windows-services usb wmi