Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF applications stop responding to touches after adding or removing tablet devices

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:

  1. Run any WPF application
  2. Keep plugging and unplugging a USB HID tablet device
  3. Make a high CPU usage

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:

  • WPF Applications Stop Responding to Touches after Adding or Removing Tablet Devices - walterlv

This article is a bit long for StackOverflow, so I only mention some conclusion here:

  • There may be a deadlock for GetPenEventMultiple posted below.
  • There may be other thread-safety issues of 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. 
like image 249
walterlv Avatar asked Aug 15 '18 07:08

walterlv


People also ask

How to fix WPF application not responding to touch?

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.

How to collect touch information from tablet devices in WPF?

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.

Why is getpeneventmultiple not working in WPF?

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.

How does the WPF stylus handle input messages?

The WPF stylus code uses the windows message loop to handle these messages: The Stylus Input thread is created by the PenThreadWorker class.


1 Answers

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.

like image 80
JohnT Avatar answered Oct 04 '22 05:10

JohnT