Skip to content

WPF Touch Event Fails When Using ManagementEventWatcher for WMI Changes #9752

Open
@lindexi

Description

@lindexi

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:

    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:

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:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Management" Version="6.0.2"/>
  </ItemGroup>
</Project>

I have uploaded a minimal reproduction project to GitHub. 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: #9706

We has the similar exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugProduct bug (most likely)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions