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?
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With