We found two way that can make the main thread locked. And we can not write any code to solve it and it can only be circumvented. The easiest way to reproduce this issue is to wait for the window in the main thread to close in the stylus input thread.
We have found two ways, the first way always happens, and the second way is probabilistic.
Before we tell you about it, we need to tell you something about the touch thread and why it can make the main thread wait forever.
Theory
The stylus input thread gets the input event when the user touches the screen.
There is a ThreadProc
method running in the stylus input thread and this method has a loop inside which will never end until the application exists.
There are nested two loops in the ThreadProc
method. In the outside one, it adds and removes PenContext
and in the inside one, it will be blocked by the PENIMC
and can be continued when the user touches the screen.
When a window is closed, it calls HwndSource.DisposeStylusInputProvider
and this causes the PenContext.Disable
be calling with the calling stack trace showing below.
Let us see the PenThreadWorker.WorkerRemovePenContext
that run in the main thread.
From the code above we can learn that the main thread releases the _pimcResetHandle
and it makes the ThreadProc
breaking the inside loop and go back to the outside one to remove the PenContext
.
Normally we should run the code in the stylus input thread to remove the PenContext
and keep the main thread waiting for the operationRemoveContext
to finish. But if the stylus input thread never remove the PenContext
and the main thread waits for it, the main thread will never continue.
The first way
The first way is to write a custom class implementing StylusPlugIn
and wait for a window to close in the OnStylusUp
method.
Let’s create a new empty window named FooWindow
.
Then we create a FooStylusPlugIn
class to implement the StylusPlugIn
with overriding the OnStylusUp
method. We add some code to wait for the window to close by calling Invoke
which will wait by pumping a new message loop.
To combine both the critical codes above, we write some codes in the MainWindow
. The FooWindow
is instanced in the constructor and the StylusPlugIn
is plugged in it. We also make a button in the XAML that can let us know whether the main thread is still running or not.
Run the project, touch the main window, and you’ll find that the main window never responds to your interaction. Try to click the button to view the responding and you’ll soon verify what I’m talking.
The reason is that the OnStylusUp
in FooStylusPlugIn
is running in the stylus input thread which is also running the inside loop of the ThreadProc
method. It needs to go back to the outside loop to remove the PenContext
when a window is closed. The stylus input thread is waiting for the main thread to close a window and the main thread is also waiting for the stylus input thread remove PenContext. Thus, the deadlock occurred.
The demo in github
The second way
If a touch happens exactly during a window closing, the main thread will enter a lock.
The difference between the first method and the second method is that the first one will lock both the main thread and the stylus input thread but the second one will only lock the main thread.
From the theory, we know that the PenContext
should be removed correctly in the outside loop. But in the second way, the stylus input thread is firing the touch event exactly when we run the code to remove the PenContext
in the stylus input thread. As you can see we need to run the code to remove PenContext
in the outside loop but at this moment the code is firing the touch event in the second loop.
The firing of the touch event means the _pimcResetHandle
is released. Although the main thread has also released the lock the code cannot run to the outside loop to remove the PenContext
and the main thread can no longer wait for the moment when the PenContext
removal is finished.
The main thread has released the lock but the stylus input thread doesn’t need to wait for the lock. The stylus input thread cannot go back to the outside loop to remove the PenContext
and main thread can no longer wait for the moment when the PenContext
removal is finished.
Thanks to walterlv for proofreading the English translation of this post.
感谢 吕毅 对本文的英文翻译进行校对。
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系。