WPF:提高在旧电脑上运行的性能
因此,我正在构建一个 WPF 应用程序并今天进行了测试部署,发现它的性能非常差。我很惊讶,因为我们在视觉效果或动画方面确实没有做太多事情。
我部署在两台机器上:运行应用程序所需的最快的和最慢的(最慢的 PC 具有 Intel Celeron 1.80GHz 和 2GB RAM)。该应用程序在较快的机器上运行得很好,但在较慢的机器上则不稳定。当我说“断断续续”时,我的意思是光标即使只是经过具有焦点的应用程序的任何打开的窗口也会跳跃。
我打开“任务管理器性能”窗口,可以看到只要应用程序获得焦点并且光标在其上移动,CPU 使用率就会跳跃。如果我将焦点转移到另一个(例如 Excel)上,CPU 使用率会在一秒钟后回落。两台机器上都发生了这种情况,但只有在速度较慢的机器上才会出现这种不稳定现象。我在部署机器上进行修补的时间非常有限,因此没有进行大量详细的测试。
该应用程序在我的开发机器上运行良好,但我也看到 CPU 飙升至 10%,只是将光标运行在窗口上。
我从 MS 下载了 WPF 性能工具,并一直在修改它(在我的开发机器上)。文档中关于 Perforator 工具中的“帧速率”指标是这样说的:
对于没有动画的应用程序, 该值应接近 0。
应用程序没有执行任何繁重的动画,但当光标位于任何窗口上时,帧速率保持在 50 附近。我测试的屏幕在网格中具有“突出显示”的列标题,以及滚动时会改变颜色和外观的按钮。即使将鼠标移动到窗口的空白区域也会导致相同的帧速率和 CPU 使用率(似乎与这些小动画无关)。
(此外,除了安装到 WPF 性能工具中的两个默认工具(Perforator 和 Visual Profiler)之外,我无法弄清楚如何获得任何东西。这可能是一个单独的问题)。
我也有 Redgate 的分析工具,但我不确定这是否可以对渲染性能有所帮助。
因此,我意识到如果没有具体细节或示例代码(我无法发布),这并不是一件容易解决的问题。我的问题是:
- 一般需要注意哪些事项 for(或避免)在代码中进行改进 表现?
- 使用 WPF 我可以采取哪些步骤 性能工具来缩小范围 问题?
- 上面列出的 PC 规格(Intel Celeron 1.80GHz,2GB RAM)是否太慢而无法运行普通的 WPF 应用程序?
So, I'm building a WPF app and did a test deployment today, and found that it performed pretty poorly. I was surprised, as we are really not doing much in the way of visual effects or animations.
I deployed on two machines: the fastest and the slowest that will need to run the application (the slowest PC has an Intel Celeron 1.80GHz with 2GB RAM). The application ran pretty well on the faster machine, but was choppy on the slower machine. And when I say "choppy", I mean the cursor jumped even just passing it over any open window of the app that had focus.
I opened the Task Manager Performance window, and could see that the CPU usage jumped whenever the app had focus and the cursor was moving over it. If I gave focus to another (e.g. Excel), the CPU usage went back down after a second. This happened on both machines, but the choppiness was only noticeable on the slower machine. I had very limited time to tinker on the deployment machines, so didn't do a lot of detailed testing.
The app runs fine on my development machine, but I also see the CPU spiking up to 10% there, just running the cursor over the window.
I downloaded the WPF performance tool from MS and have been tinkering with it (on my dev machine). The docs say this about the "Frame Rate" metric in the Perforator tool:
For applications without animation,
this value should be near 0.
The app is not doing any heavy animation, but the frame rate stays near 50 when the cursor is over any window. The screens I tested on have column headers in a grid that "highlight" and buttons that change color and appearance when scrolled over. Even moving the mouse on blank areas of the windows cause the same Frame rate and CPU usage (doesn't seem to be related to these minor animations).
(Also, I am unable to figure out how to get anything but the two default tools--Perforator and Visual Profiler--installed into the WPF performance tool. That is probably a separate question).
I also have Redgate's profiling tool, but I'm not sure if that can shed any light on rendering performance.
So, I realize this is not an easy thing to troubleshoot without specifics or sample code (which I can't post). My questions are:
- What are some general things to look
for (or avoid) in the code to improve
performance? - What steps can I take using the WPF
performance tool to narrow down the
problem? - Is the PC spec listed above (Intel Celeron 1.80GHz with 2GB RAM) too slow to be running even vanilla WPF applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对于 WPF 来说是不正常的 - 我怀疑您的一位开发人员编写了在后台运行计时器的代码(或更可能根据您的描述,是鼠标移动处理程序),这会以某种方式影响 UI。
如果你有 ANTS 性能分析器(它真的很好),我会在你的应用程序上运行它并重现问题。
完成此操作后,ANTS 应该很快告诉您问题是什么。
如果 ANTS 根本没有透露任何信息,并且向您显示实际上您的代码在此期间没有运行,那么我会怀疑显卡驱动程序有问题。
您可以通过设置以下注册表项禁用硬件加速并重试来对此进行测试:
注意:
DisableHWAcceleration
值应为DWORD
This is not normal for WPF - I'd suspect one of your developers has written code that runs a timer in the background (or more likely given your description, a mouse move handler) which is affecting the UI in some way.
If you have ANTS performance profiler (it's really nice) I'd run that over your app and reproduce the problem.
Once you've done that, ANTS should tell you fairly quickly what the problem is.
If ANTS doesn't reveal anything at all, and shows you that in fact none of your code is running during this time, then I'd suspect buggy graphics card drivers.
You can test for this by disabling hardware acceleration by setting the following registry key, and trying again:
Note: the
DisableHWAcceleration
value should be aDWORD
您是否将任何
BitmapEffect
-s 应用于您的 UI 元素?它们不由 GPU 处理,因此 CPU 负责渲染它们。如果使用不当(例如,将 OuterGlowBitmapEffect 应用于大型复杂元素),它们可能会对性能产生可怕的影响。
此外,您可能仍然想尝试使用性能分析器来分析您的应用程序。只是想看看是否是您的代码导致了此问题。
Are you applying any
BitmapEffect
-s to your UI elements?They are not handled by GPU, so CPU takes care of rendering them. If not used properly (e.g. having a OuterGlowBitmapEffect applied to a large complex element) they can have terrible impact on performance.
Also, you still might want to try profiling your app with a performance profiler. Just to see if it's not your code that causes this.