Run any WPF application on a computer which is currently has a high CPU usage, if you keep plugging and unplugging a USB HID tablet device at the same time, the WPF applications will stop responding to touches and only respond to mouse.
The requirements:
My question:
Is there any thorough way for us non-Microsoft developers do to fix this touch failure?
I've posted the preliminary analysis of the touch failure here:
This article is a bit long for StackOverflow, so I only mention some conclusion here:
GetPenEventMultiple
posted below.GetTabletInfoHelper
.The code below is from .NET Framework and I simplified them for easier understanding:
// PenThreadWorker.ThreadProc while(There are two loops in real) { // The `break` below only exit one loop, not two. if (this._handles.Length == 1) { if (!GetPenEvent(this._handles[0], otherArgs)) { break; } } else if (!GetPenEventMultiple(this._handles, otherArgs)) { break; } // Other logics. } // WorkerOperationGetTabletsInfo.OnDoWork try { _tabletDeviceInfo = PenThreadWorker.GetTabletInfoHelper(pimcTablet); } catch(COMException) { _tabletDevicesInfo = new TabletDeviceInfo[0]; } catch(ArgumentException) { _tabletDevicesInfo = new TabletDeviceInfo[0]; } // Other exception handling.
Run any WPF application with a high CPU usage, and then keep plugging and unplugging a USB HID tablet device, you’ll get the WPF application which stops responding to touches. If multiple WPF applications are running at the same time, most of them will lose touch.
WPF use two different threads to collect touch information from tablet devices and convert them to the Stylus and Mouse event that most of us are familiar to. The Main thread of WPF. WPF use windows message loop on this thread to handle mouse message and device change message. The Stylus Input thread.
So the infinite waiting of GetPenEventMultiple is actually a deadlock which is also a thread-safety issue. Remember that we can make a high CPU usage to increase the probability of reproduction, we can infer that the touch failure issue is caused by the thread-safety issue of WPF stylus handling.
The WPF stylus code uses the windows message loop to handle these messages: The Stylus Input thread is created by the PenThreadWorker class.
Your operating system will ultimately crash, bog down due to resource usage, or otherwise corrupt memory if the hardware device is repeatedly connected and disconnected.
Suggest you change the WPF desktop application to do nothing until the user connects the tablet and clicks on a button in the WPF desktop application to "Enable Tablet" support.
Otherwise, you will get some user with a bad USB cable repeatedly connecting and disconnecting the tablet.
The OS won't be able to handle the device the same as if it is a faulty disk controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With