2018-11-1-WPF-Main-thread-gets-a-deadlock-when-stylus-input-thread-is-waiting-for-the-window-to-clos...
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
WPF Main thread gets a deadlock when stylus input thread is waiting for the window to close
|
lindexi
|
2018-11-01 09:32:42 +0800
|
2018-10-30 16:57:37 +0800
|
WPF
|
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.
void ThreadProc()
{
// The loop that never ends.
while (!__disposed)
{ }
}
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.
void ThreadProc()
{
while (!__disposed)
{
// The outside loop
// To remove or add the PenContext while (true)
{
// The inside loop
// Tt will be blocked by the PENIMC
if(!Penimc.UnsafeNativeMethods.GetPenEvent(/*the thread locker*/))
{
// If the `_pimcResetHandle` is released, this if branch will enter so the inside loop will end with the `break` and the code runs back to the outside loop.
break;
} FireEvent(/*fire the touch events*/);
}
}
}
When a window is closed, it calls HwndSource.DisposeStylusInputProvider and this causes the PenContext.Disable be calling with the calling stack trace showing below.
System.Windows.Input.PenThreadWorker.WorkerRemovePenContext(System.Windows.Input.PenContext penContext)
System.Windows.Input.PenContext.Disable(bool shutdownWorkerThread)
System.Windows.Input.PenContexts.Disable(bool shutdownWorkerThread)
System.Windows.Input.StylusWisp.WispLogic.UnRegisterHwndForInput(System.Windows.Interop.HwndSource hwndSource)
System.Windows.Interop.HwndStylusInputProvider.Dispose()
Let us see the PenThreadWorker.WorkerRemovePenContext that run in the main thread.
internal bool WorkerRemovePenContext(PenContext penContext)
{
var operationRemoveContext = new PenThreadWorker.WorkerOperationRemoveContext(penContext, this); _workerOperation.Add((PenThreadWorker.WorkerOperation) operationRemoveContext);
// Release the _pimcResetHandle lock
UnsafeNativeMethods.RaiseResetEvent(this._pimcResetHandle.Value);
// Wait for the operationRemoveContext to finish
operationRemoveContext.DoneEvent.WaitOne();
operationRemoveContext.DoneEvent.Close();
return operationRemoveContext.Result;
}
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.
public class FooWindow : Window
{ }
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.
public class FooStylusPlugIn : StylusPlugIn
{
public FooStylusPlugIn(FooWindow fooWindow)
{
FooWindow = fooWindow;
} public FooWindow FooWindow { get; } /// <inheritdoc />
protected override void OnStylusUp(RawStylusInput rawStylusInput)
{
FooWindow.Dispatcher.Invoke(() => FooWindow.Close());
base.OnStylusUp(rawStylusInput);
}
}
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.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_fooWindow = new FooWindow();
StylusPlugIns.Add(new FooStylusPlugIn(_fooWindow));
_fooWindow.Show();
} private void Button_OnClick(object sender, RoutedEventArgs e)
{
} private FooWindow _fooWindow;
}
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.
void ThreadProc()
{
while (!__disposed)
{
// The outside loop
// To remove or add the PenContext
// The main thread is waiting for its finishing.
RemovePenContext(); while (true)
{
// The inside loop
// Tt will be blocked by the PENIMC
if(!Penimc.UnsafeNativeMethods.GetPenEvent(/*wait the lock*/))
{
// If the `_pimcResetHandle` is released, this if branch will enter so the inside loop will end with the `break` and the code runs back to the outside loop.
break;
} FireEvent(/*fire the touch events*/); // the code is running in this line
// and the `_pimcResetHandle` is released.
// the main thread release the `_pimcResetHandle` but the code can not go to RemovePenContext for it will no longer break.
}
}
}
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.
感谢 吕毅 对本文的英文翻译进行校对。
2018-11-1-WPF-Main-thread-gets-a-deadlock-when-stylus-input-thread-is-waiting-for-the-window-to-clos...的更多相关文章
- OI生涯回忆录 2018.11.12~2019.4.15
上一篇:OI生涯回忆录 2017.9.10~2018.11.11 一次逆风而行的成功,是什么都无法代替的 ………… 历经艰难 我还在走着 一 NOIP之后,全机房开始了省选知识的自学. 动态DP,LC ...
- China Intelligent Office Summit(2018.11.21)
时间:2018.11.21地点:中关村软件园国际会议中心
- International Programming Retreat Day(2018.11.17)
时间:2018.11.17地点:北京国华投资大厦
- Intel Artificial Intelligence Conference(2018.11.14)
时间:2018.11.14地点:北京国贸大酒店
- Debian系统中当安装deb软件时出现:deb cdrom:[Debian GNU/Linux 9.3.0 _Stretch_ - Official amd64 DVD Binary-1 20171209-12:11]/ stretch contrib main
vi /etc/apt/sources.list // 注释掉下面这句话# deb cdrom:[Debian GNU/Linux 9.3.0 _Stretch_ - Official amd64 D ...
- 2018.11.23 浪在ACM 集训队第六次测试赛
2018.11.23 浪在ACM 集训队第六次测试赛 整理人:刘文胜 div 2: A: Jam的计数法 参考博客:[1] 万众 B:数列 参考博客: [1] C:摆花 参考博客: [1] D:文化之 ...
- 2018.11.16 浪在ACM 集训队第五次测试赛
2018.11.16 浪在ACM 集训队第五次测试赛 整理人:李继朋 Problem A : 参考博客:[1]朱远迪 Problem B : 参考博客: Problem C : 参考博客:[1]马鸿儒 ...
- 2018.11.9浪在ACM集训队第四次测试赛
2018.11.9浪在ACM集训队第四次测试赛 整理人:朱远迪 A 生活大爆炸版 石头剪刀布 参考博客:[1] 刘凯 B 联合权值 参考博客: [1]田玉康 ...
- 2018.11.2浪在ACM集训队第三次测试赛
2018.11.2 浪在ACM 集训队第三次测试赛 整理人:孔晓霞 A 珠心算测试 参考博客:[1]李继朋 B 比例简化 参考博客: [1]李继朋 C 螺旋矩阵 参考博客:[1]朱远迪 D 子矩阵 ...
- Thread.Sleep(0) vs Sleep(1) vs Thread.Yeild()
注:本文为个人学习摘录,原文地址:http://www.cnblogs.com/stg609/p/3857242.html 本文将要提到的线程及其相关内容,均是指 Windows 操作系统中的线程,不 ...
随机推荐
- pb_ds(平板电视)简介
据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...
- 2019牛客暑假多校赛(第二场) F和H(单调栈)
F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...
- 《DSP using MATLAB》Problem 8.10
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- java基础之DateFormat类
DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...
- Django项目: 项目环境搭建 ---- 三、在码云平台创建项目&推送到码云上
三.在码云平台创建项目 git服务平台: 主要使用github(最主流) 国内访问速度慢 托管私有项目收费 国内一般使用码云gitee 国内访问速度快 托管私有项目免费(限制开发人数) 公司中使用gi ...
- elasticsearch 中文API 索引(三)
索引API 索引API允许开发者索引类型化的JSON文档到一个特定的索引,使其可以被搜索. 生成JSON文档 有几种不同的方式生成JSON文档 利用byte[]或者作为一个String手动生成 利用一 ...
- 转载:Linux 安装Java
1.到官网下载 jdk-8u131-linux-x64.tar.gz 官网地址:http://www.Oracle.com/technetwork/java/javase/downloads/jdk8 ...
- centos Python2.6 升级到2.7
需求: centos 6.x 系统默认Python 版本都是2.6,实际生产环境中需要用到Python 2.7.x Python 2.7 下载地址 [root@ansible package]# wg ...
- hdu6277
hdu6277结论题 #include<iostream> #include<cstdio> #include<queue> #include<algorit ...
- c++类成员函数的重载和覆盖有什么区别
1.成员函数被重载的特征: (1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual 关键字可有可无. 2.覆盖是指派生类函数覆盖基类函数,特征是: (1)不同 ...