Determine what is blocking UI thread
Determine what is blocking UI thread
问题
I am working on a rather large .NET WPF real-time application. The application is working great and as expected, except for one BIG issue - UI Update is slow.
This application is highly event driven, there are events raised all over for all sorts of things - through these events the UI is updated.
One or many of these events is blocking the UI from displaying immediately. When all the work is complete, the UI shows the expected results.
Is there any way of determining which event handler is causing the bottleneck?
回答1
public class UIBlockDetector
{
static Timer _timer;
public UIBlockDetector(int maxFreezeTimeInMilliseconds = 200)
{
var sw = new Stopwatch();
new DispatcherTimer(TimeSpan.FromMilliseconds(10), DispatcherPriority.Send, (sender, args) =>
{
lock (sw)
{
sw.Restart();
}
}, Application.Current.Dispatcher);
_timer = new Timer(state =>
{
lock (sw)
{
if (sw.ElapsedMilliseconds > maxFreezeTimeInMilliseconds)
{
// Debugger.Break() or set breakpoint here;
// Goto Visual Studio --> Debug --> Windows --> Theads
// and checkup where the MainThread is.
}
}
}, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(10));
}
}
Just new this class in MainWindow constructor. When the breakpoint hits, you can go to Visual Studio --> Debug --> Windows --> Threads and check what operation blocked your UI-Thread!
回答2
I fully support colithium's suggestion of using a profiler.
In addition, if the blocking takes more than a second, you might be able to hit the "Pause" button in Visual Studio. In the tool bar, there's a dropdown list where you can choose the "Main Thread". Then it jumps to the method which is currently blocking the UI.
回答3
Do you have access to a code profiler? This is the type of thing they are good at. I recommend obtaining one if the answer is no.
Besides using a profiler. You can do "poor man's" profiling by placing timing statements at the beginning and end of code blocks that you suspect. You can even use a breakpoints and time it with a wall clock. Does the issue happen when you click something? If so start there. Is it a recurring issue without user interaction? Start with timers then.
As for actually solving the problem... Unless the offending handler is doing something that can be made more efficient, consider adopting a multi-threaded approach. The new Task library for .NET 4.0 is really amazing in this regard.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-11 TypeScript安装
2019-07-11 How does ASP.NET Forms Authentication really work?
2019-07-11 forms authentication原理
2019-07-11 Adding property to a json object in C#
2019-07-11 jQuery file upload 服务端返回数据格式
2019-07-11 What exactly is the parameter e (event) and why pass it to JavaScript functions?
2019-07-11 jQuery .submit()