WPF Touch Event Fails When Using ManagementEventWatcher for WMI Changes
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
When using `ManagementEventWatcher` to listen for WMI changes in a WPF application, the touch functionality becomes unresponsive.
### Reproduction Steps
1. Install the `System.Management` library to use `WqlEventQuery` for monitoring WMI changes.
2. Attach a breakpoint to the `TouchDown` event to log information.
The complete code for reproduction is as follows:
```csharp
public MainWindow()
{
InitializeComponent();
AppDomain.CurrentDomain.FirstChanceException += (sender, args) =>
{
Debug.WriteLine(args.Exception);
};
WqlEventQuery insertQuery =
new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'");
ManagementEventWatcher insertWatcher = new ManagementEventWatcher(insertQuery);
insertWatcher.Start(); // When the line `insertWatcher.Start();` is commented out, touching the window hits the breakpoint in the `MainWindow_TouchDown` method. However, after executing `insertWatcher.Start();`, touching the window does not hit the `MainWindow_TouchDown` method.
TouchDown += MainWindow_TouchDown;
}
private void MainWindow_TouchDown(object? sender, TouchEventArgs e)
{
Debugger.Break(); // Never hit
}
```
When the line `insertWatcher.Start();` is commented out, touching the window hits the breakpoint in the `MainWindow_TouchDown` method. However, after executing `insertWatcher.Start();`, touching the window does not hit the `MainWindow_TouchDown` method.
Additionally, a `FirstChanceException` is thrown, with the following exception details:
```plaintext
System.InvalidCastException: Interface not registered
at MS.Win32.Penimc.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
```
Here is the content of the csproj file used in my code:
```xml
WinExe
net9.0-windows
enable
enable
true
```
I have uploaded a minimal reproduction project to [GitHub](https://github.com/lindexi/lindexi_gd/tree/308096e0c8ede019f6dbe5bfe974ae1a12d7de42/WPFDemo/YanerehaylemJeekalhebel). You can clone the project to reproduce the issue.
### Expected behavior
It can work well and receive the touch event
### Actual behavior
The TouchDown event never be raised.
This issues only happend in .NET Core not the .NET Framework.
### Regression?
_No response_
### Known Workarounds
The touch event can be raised after open the WM_Pointer support, because the PointerLogic do not need the COM.
### Impact
_No response_
### Configuration
OS version: Windows 11 23H2 22631.4037
### Other information
Reference: https://github.com/dotnet/wpf/issues/9706
We has the similar exception.
Contributor guide
Assessment
This issue has not been assessed yet.