windows 休眠后未触发 WMI InstanceCreationEvent

WMI InstanceCreationEvent not fired after windows hibernate

我创建了两个处理程序来接收 USB 设备 plug/unplug 事件,类似于 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,然后是一个 InstanceCreationEvent 用于 USB 设备。

然而,在少数计算机上,在 windows 休眠和唤醒后,我只能收到 InstanceDeletionEvent,而同一设备没有 InstanceCreationEvent。 USB 设备保持在 USB 端口上不变。

如果我关闭并重新启动我的应用程序,或 unplug/plug 同一台设备,上述代码将再次运行。

USB设备在某些环境下休眠后InstanceDeletionEvent而不是InstanceCreationEvent是否有问题?

有一个名为 ResetOnResume 的注册表项,它将强制 USB 驱动程序堆栈在唤醒时重置设备。默认情况下它是禁用的,这可以解释您在某些 PC 上从休眠状态唤醒后遇到的问题。

ResetOnResume

Indicates whether the USB driver stack must reset the device when the port resumes from a sleep cycle.

我找到了答案,但似乎与软件无关。

在那些有问题的 PC 上,如果 USB 设备直接插入主板上的 USB 端口 InstanceDeletionEventInstanceCreationEvent 将正常启动从休眠中醒来后,工作机会超过 95%。

如果 USB 设备插入前面板的 USB 端口,有大约 75% 的几率 InstanceCreationEvent 不会在唤醒后触发。

可能是硬件问题。