High CPU usage when using ErrorProvider in WinForms, why?
I've got an application that goes into high CPU usage (slowly over time) when the ErrorProvider control sets the blinking icon continuously on several controls.
The high CPU usage is not immediate but have a slow ramp until the application hits 100% CPU.
When there is no error shown then CPU goes back to normal. Is it normal that the ErrorProvider takes so mu开发者_Go百科ch CPU time?
This isn't normal. In all likelihood you are leaking memory or window handles. Start diagnosing this with Taskmgr.exe, Processes tab. View + Select Columns and tick Memory (Commit size), Handles, USER objects and GDI objects. Observe the values of these columns while your app runs.
Steadily increasing values indicates a problem in your code that can drive up the cpu usage. A pretty classic problem is leaking USER handles, induced by removing controls from your form with Controls.Remove() or Controls.Clear() and forgetting to dispose those controls.
According to this MS connect bug report: Graphical Resources Leak In ErrorProvider
When there is an error associated with a control, the ErrorProvider component displays a blinking icon on its right. I've discovered that every time the icon blinks, an new DeviceContext is added to the System.Drawing.Internal.DeviceContexts internal list. And when I call the Dispose() method on the ErrorProvider object, the DeviceContexts list is not cleared.
Consequence : by default, the icon blinks every 250 ms so 4 DeviceContext objects are added to the System.Drawing.Internal.DeviceContexts list per second. When an ErrorProvider is active and the BlinkStyle is AlwaysBlink, the list grows indefinitely ... and when this list is very big (I've seen it with 500000+ objects!) the application drawing operations are very very very slow :(
This bug is marked as fixed but the most recent comment says it is under investigation.
精彩评论