最近一个同事遇到进度条加载不出来问题,即使偶尔加载出来了却不显示进度,
看到这个问题想到的肯定是把UI线程给占住了, 由于使用了几个框架,简单查看框架后,在框架中改为线程调用 问题解决了,
但是在思考一个问题,框架中的代码我是能够看到也可以修改,如果是不能更改的框架怎么办?
研究了一下,在需要用的地方调用 DispatcherHelper.UpdateUI();
PS: 在WPF中一些操作卡主UI,是WPF程序员还是喜欢winform方式,不喜欢用绑定,造成的问题。
public
static class DispatcherHelper
{
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void UpdateUI()
{
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback((agr) =>
{
((DispatcherFrame)agr).Continue = false;
return null;
}), frame);
try
{
Dispatcher.PushFrame(frame);
}
catch (Exception ex)
{
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4415646/blog/4415745