-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
- .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.
wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusLogic.cs
Lines 417 to 435 in ab8e3f0
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.
wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusLogic.cs
Line 427 in ab8e3f0
_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:
- Create the application and attache the StylusPlugIn to main window
- Run the application in the device with touch screen.
- 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