WPF BeginInvoke GUI冻结 [英] WPF BeginInvoke GUI Freeze

查看:76
本文介绍了WPF BeginInvoke GUI冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在以下代码中遇到问题:



i have problem in the following code:

System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
    Generate();
    viewer.c1DocumentViewer.Dispatcher.BeginInvoke((Action)(() =>
    {
         viewer.Report = this;
    }));
}));
t.IsBackground = true;
t.Start();



在生成过程中,generate函数会生成一个大约6页的报告
一切正常,我的GUI不会冻结.
但是当我调用BeginInvoke在报表查看器中设置生成的报表时,
整个GUI冻结大约6秒钟.
如何避免冻结gui?

预先感谢,
manu



The generate function generates a Report with about 6 pages, during the generation
everything works fine and my GUI does not freeze.
But when i call BeginInvoke to set my generated Report in the Report Viewer
the whole GUI freezes for about 6 seconds.
How can i avoid freezing my gui?

Thanks in advance,
manu

推荐答案

此代码可以改进,但我认为您无法轻松地使UI更具响应性.基本上,您认为正确.只是一件事:在这种情况下,InvokeBeginInvoke好一点,但是我认为您看不出有什么大不同(但是尝试一下).用这种方法创建线程很昂贵(有三种选择更快),但是在调用viewer属性之前,您没有报告任何可观察到的延迟.

看起来唯一耗时的操作是viewer内容的呈现,但是无论如何,您应该按照正确的方式通过分派器执行分配.并且渲染本身是在单独的渲染线程中完成的.

我将尝试以下操作:如果大小相同,是否可以生成新报告,然后将其重新分配给查看器?是否需要相同的时间?如果总是一样,我会考虑从一个简短的内容开始,然后从单独的线程中逐一附加小片段.这将使总时间更长,但可以使UI保持响应.毕竟,我不知道为什么6页这么慢.也许简单的原因是C1DocumentViewer类很烂. :-)

-SA
This code can be improved, but I don''t think you can easily make UI more responsive. Basically, you think correctly. Just one thing: in this case, Invoke is a bit better then BeginInvoke, but I don''t think you can see a big difference (but try it). Creation of a thread this way is expensive (there are three alternatives which are faster), but you did not report any observable delay before the invocation of the viewer property.

It looks like the only time-consuming operation is the rendering of the viewer content, but you the assignment should be performed through the dispatcher as you correctly do, anyway; and the rendering itself is done in a separate rendering thread.

I would experiment with the following: could you generate a new report if the same size and assign it to the viewer again. Does it take the same time? If it always the same, I would think about starting with a very short content and appending small pieces one by one from your separate thread. It would make total time even more, but could keep the UI responsive. After all, I don''t know why 6 pages could be so slow. Maybe the simple reason is that the C1DocumentViewer class sucks. :-)

—SA


Thread 上有一个Priority 属性(
There is a Priority property on the Thread ([^]). I If you reduced the priority, maybe reducing the priority will help.


这篇关于WPF BeginInvoke GUI冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆