Skip to content

WPF tocuh in Window with StylusPlugIn may throw InvalidOperationException  #6829

Open

Description

@lindexi
  • .NET Core Version: 6.0.121.56705
  • Windows version: win10 21H2
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No

Problem description:

After #2891 the Stylus Input thread will call the GetAndCacheTransformToDeviceMatrix to get the Matrix to calculate.

protected Matrix GetAndCacheTransformToDeviceMatrix(PresentationSource source)
{
var hwndSource = source as HwndSource;
Matrix toDevice = Matrix.Identity;
if (hwndSource?.CompositionTarget != null)
{
// If we have not yet seen this DPI, store the matrix for it.
if (!_transformToDeviceMatrices.ContainsKey(hwndSource.CompositionTarget.CurrentDpiScale))
{
_transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale] = hwndSource.CompositionTarget.TransformToDevice;
Debug.Assert(_transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale].HasInverse);
}
toDevice = _transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale];
}
return toDevice;
}

After we changed the moniter DPI and then we touch the application immediately, the GetAndCacheTransformToDeviceMatrix will call hwndSource.CompositionTarget.TransformToDevice and then throw the System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

_transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale] = hwndSource.CompositionTarget.TransformToDevice;

Because we can not find the new DpiScale in _transformToDeviceMatrices

Actual behavior:

Application: Application.exe
CoreCLR Version: 6.0.121.56705
.NET Version: 6.0.1
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.ThrowVerifyAccess()
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.Threading.DispatcherObject.VerifyAccess()
   at System.Windows.Media.CompositionTarget.VerifyAPIReadOnly()
   at System.Windows.Interop.HwndTarget.get_TransformToDevice()
   at System.Windows.Input.StylusLogic.GetAndCacheTransformToDeviceMatrix(PresentationSource source)
   at System.Windows.Input.StylusWisp.WispLogic.GetTabletToViewTransform(PresentationSource source, TabletDevice tabletDevice)
   at System.Windows.Input.PenContexts.InvokeStylusPluginCollection(RawStylusInputReport inputReport)
   at System.Windows.Input.StylusWisp.WispLogic.InvokeStylusPluginCollection(RawStylusInputReport inputReport)
   at System.Windows.Input.StylusWisp.WispLogic.ProcessInputReport(RawStylusInputReport inputReport)
   at System.Windows.Input.StylusWisp.WispLogic.ProcessInput(RawStylusActions actions, PenContext penContext, Int32 tabletDeviceId, Int32 stylusDeviceId, Int32[] data, Int32 timestamp, PresentationSource inputSource)
   at System.Windows.Input.PenContexts.ProcessInput(RawStylusActions actions, PenContext penContext, Int32 tabletDeviceId, Int32 stylusPointerId, Int32[] data, Int32 timestamp)
   at System.Windows.Input.PenContexts.OnPenDown(PenContext penContext, Int32 tabletDeviceId, Int32 stylusPointerId, Int32[] data, Int32 timestamp)
   at System.Windows.Input.PenContext.FirePenDown(Int32 stylusPointerId, Int32[] data, Int32 timestamp)
   at System.Windows.Input.PenThreadWorker.FireEvent(PenContext penContext, Int32 evt, Int32 stylusPointerId, Int32 cPackets, Int32 cbPacket, IntPtr pPackets)
   at System.Windows.Input.PenThreadWorker.ThreadProc()
   at System.Threading.Thread.StartHelper.Callback(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Thread.StartCallback()

Expected behavior:

Work well.

Minimal repro:

  1. Create the application and attache the StylusPlugIn to main window
  2. Run the application in the device with touch screen.
  3. Touch the main window and then change the moniter DPI and then touch the application immediately

Repeat step 3 many times, and we will find the exception.

Reference: #6415

cc @rladuca

Activity

  1. lindexi commented on Jul 20, 2022

    @lindexi
    MemberAuthor

    Why we cache the Matrix to _transformToDeviceMatrices ? Why not calculate the Matrix with hwndSource.CompositionTarget.CurrentDpiScale ?

    The TransformToDevice property is calculate with CurrentDpiScale.

    public override Matrix TransformToDevice
    {
    get
    {
    VerifyAPIReadOnly();
    Matrix m = Matrix.Identity;
    m.Scale(CurrentDpiScale.DpiScaleX, CurrentDpiScale.DpiScaleY);
    return m;
    }
    }

  2. added this to the Future milestone on Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚧 work in progressBugarea-Touch

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions