33

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:

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.
Payam Khaninejad
  • 7,692
  • 6
  • 45
  • 55
walterlv
  • 2,366
  • 13
  • 73
  • @Dylan All of the devices may make the `PenThreadWorker.GetTabletInfoHelper` throw some exception that will make WPF cant create pen context. – lindexi Aug 30 '18 at 01:51
  • @Dylan For WPF will re-init the tablet collection and WPF will call the `PenThreadWorker.WorkerOperation` to use `PenThreadWorker.WorkerOperationGetTabletsInfo` but `PenThreadWorker.GetTabletInfoHelper` in `WorkerOperationGetTabletsInfo.OnDoWork` may throw `COMException` that will create an empty TabletDeviceInfo. And the pen context cant be create by `TabletDevicesInfo` is empty and the WPF cant get any touch for pen context array is empty. – lindexi Aug 30 '18 at 01:52
  • I did have some problems with the touch on buttons and such on Tablets, but everthing did work without any problems with the mouse, how are u creating the event's? `Click=""` or with `PreviewMouseLeftButtonDown=""` (or similar). After I did change them for the last example given, I could put the dock, remove it and such and the clicks always did work. Don't know if it will help out, but it did fix the problems that I was having with WPF click events on tablets – Camadas Apr 29 '19 at 15:15
  • @Camadas That is no means that changing the touch event can help. The WPF application can not receive the touch event. Can you give an example to fix the touch when WPF stop responding the touch. – lindexi Apr 30 '19 at 07:51
  • @lindexi maby this can help out, https://github.com/jaytwo/WmTouchDevice on the file `MainWindow.xaml.cs` that is on WpfSample that goes from the line 22 to 41. as it says on the readme : "WmTouchDevice contains a basic implementation of WPF's TouchDevice class based on the data received in WM_TOUCH window messages rather than the Tablet PC API". This did solve my problem when filling DataGrid with lots of data, the touch would stop working (on table) while on the desktop I didn't have any problems – Camadas Apr 30 '19 at 10:30
  • @Camadas Thanks and I read the code and I think it should write the input code. And it should change all the exits code. – lindexi May 10 '19 at 00:27
  • @lindexi Any time, that lines of code did wonders on a large data being fed into a DataGrid (I did even have the `EnableRowVirtualization="True"` alleviate the burden on the DataGrid), solved my problem on touch event's not working while on desktop was working without any problems – Camadas May 10 '19 at 10:20

1 Answers1

1

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.

JohnT
  • 69
  • 4